2 loveiflower 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
zhanglei5415   Rxr 2013.08.13 16:05
已采纳

问题出在这里

incomePerHour = resultNumber;

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

改成

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

一楼说的对,这个是数据类型不匹配

Csdn user default icon
上传中...
上传图片
插入图片