loveiflower 于 2013.08.13 15:25 提问

objective-c中NSNumber问题

``````Incompatible pointer types assigning to 'UITextfield *_strong' from 'NSNumber *_strong
``````

.h文件

``````@property (weak, nonatomic) IBOutlet UITextField *initialBudget;
@property (weak, nonatomic) IBOutlet UITextField *expenses;
@property (weak, nonatomic) IBOutlet UITextField *timeSpent;
@property (weak, nonatomic) IBOutlet UITextField *incomePerHour
``````

``````- (IBAction)calculateResults:(id)sender {
double budget = [initialBudget.text doubleValue ];
double expense = [expenses.text doubleValue];
double time = [timeSpent.text doubleValue];
double hourlyIncome = (budget - expense)/time;
NSNumber *resultNumber = [[NSNumber alloc] initWithDouble:hourlyIncome];
incomePerHour = resultNumber;
};
``````

2个回答

zhanglei5415   2013.08.13 16:05

``````incomePerHour = resultNumber;
``````

incomePerHour为UITextField 类型,而resultNumber为 NSNumber 类型。这两个不着边的对象怎可相互转换？

``````incomePerHour.text=[NSString stringWithFormat:@"%f",hourlyIncome];
``````
I_The_God   2016.07.08 21:32

Objective-c之NSNumber与int之间的转换
int 转 NSNumber: [NSNumber numberWithInt:(int)]; NSNumber 转 int [(NSNumber) intValue]; 其他数据类型类似 有些数组只能存Object对象，所以需要将普通数据类型转换成为Object型。
Objective-c中数字型字符串转换NSNumber的三种方法

【Objective-C】OC中NSNumber数字对象的常用处理方法

Objective-C中使用NSValue、NSNull、NSNumber、NSDecimalNumber、NSData、NSMutableData 小结

[Objective-C] NSArray/NSDictionary/NSNumber 的语法糖

NSNumber *a1 = [NSNumber numberWithUnsignedInt:2];//long NSNumber *b1 = [NSNumber numberWithBool:YES];//char||BOOL NSNumber *c1 = [NSNumber numberWithChar:'a'];//char
Objective-C中NSString转NSNumber的方法

objective-c 中数据类型之五 数值类（NSNumber及NSNumberFormatter）

Objective-C中的BOOL/bool/Boolean/NSCFBoolean区别