我写了这段代码:
for (int i = 1; i < 1000; i++) {
for (int j = 1; j < 1000; j++) {
int k = i * j;
//This is the line were the error messages appears
NSString *number = [NSString stringWithFormat:@"%d", k];
NSNumber *length = number.length;
NSNumber *half = (length / 2);
我想要它做的是采用int k
,将其转换为名为number
的NSString,将字符串number
的长度存储在NSNumber {{1}中和NSNumber length
中该值的一半。
但是,在尝试将int half
转换为NSString k
时,我收到错误消息:
“不允许将'NSInteger'(又名'unsigned int')隐式转换为'NSNumber *'并使用arc”
有人可以帮我找出原因吗?
答案 0 :(得分:3)
number.length
返回的整数不是NSNumber
。
纠正于:
NSInteger length = number.length;
NSInteger half = (length / 2);
如果你真的想使用NSNumber,请这样做:(这里我将整数装箱到NSNumber对象)
NSNumber *length = @(number.length); //same as [NSNumber numberWithInt:number.length]
NSNumber *half = @(number.length / 2);