我正在尝试制作年龄计数器,但第一行有错误:
"Invalid operands to binary expression ('int' and 'NSString')"
-(IBAction)OK:(id)sender; {
Age = 2013 - textfield.text ;
label.text = [NSString stringWithFormat:@"%i" , Age];
最佳答案
你需要取文本的整数值,然后你可以用它做减法。但如果字符串不可转换为 int(例如“this”不是 int...),NSString 的 intValue 可能返回 0。
-(IBAction)OK:(id)sender; {
Age = 2013 - [textfield.text intValue] ;
label.text = [NSString stringWithFormat:@"%i" , Age];
}
关于ios - 二进制表达式 ('int' 和 'NSString' 的无效操作数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21240103/