如何使用objective-c中字典中存储的浮点运算?
我有如何使用objective-c中字典中存储的浮点运算?,objective-c,Objective C,我有NSDictionary*结果 当我这样做时: self.tValue.text = [NSString stringWithFormat:@"%@", [results objectForKey:@"temperature"] + 273.15f]; 我得到一个错误: Invalid operands for binary expression ('id _Nullable' and 'float') 我错在哪里?存储在NSDictionary中的“float”很可能是一个NSNumb
NSDictionary*结果
当我这样做时:
self.tValue.text = [NSString stringWithFormat:@"%@", [results objectForKey:@"temperature"] + 273.15f];
我得到一个错误:
Invalid operands for binary expression ('id _Nullable' and 'float')
我错在哪里?存储在NSDictionary中的“float”很可能是一个NSNumber(或者可能是一个NSString)——它实际上不可能是一个“float”。因此,您需要获取NSDictionary值的“floatValue”,进行加法,然后使用“float”字符串格式
请尝试以下操作:
self.tValue.text = [NSString stringWithFormat:@"%f", [results[@"temperature"] floatValue] + 273.15f];
NSDictionary中存储的“float”很可能是一个NSNumber(或者可能是一个NSString)——它实际上不可能是一个“float”。因此,您需要获取NSDictionary值的“floatValue”,进行加法,然后使用“float”字符串格式
请尝试以下操作:
self.tValue.text = [NSString stringWithFormat:@"%f", [results[@"temperature"] floatValue] + 273.15f];
在添加id对象的浮点值之前,必须将
温度值转换为如下浮点值:
CGFloat temperature = [[results objectForKey:@"temperature"] floatValue];
NSString *tValue = [NSString stringWithFormat:@"%f", temperature + 273.15f];
在添加id对象的浮点值之前,必须将温度值转换为如下浮点值:
CGFloat temperature = [[results objectForKey:@"temperature"] floatValue];
NSString *tValue = [NSString stringWithFormat:@"%f", temperature + 273.15f];