ios 在目标 C 中进行比较 - ARC 不允许将“int”隐式转换为“id”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21603609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing in objective C - Implicit conversion of 'int' to 'id' is disallowed with ARC
提问by Mads K
I i'm getting the error "Implicit conversion of 'int' to 'id' is disallowed with ARC" at the line marked with "faulty line". I guess it have something to do with that i'm checking for an integer in an array, that contains objects instead of integers.
我在标有“故障行”的行收到错误“ARC 不允许将‘int’隐式转换为‘id’”。我想这与我正在检查数组中的整数有关,该数组包含对象而不是整数。
#import "RandomGenerator.h"
@implementation RandomGenerator
NSMutableArray *drawnNumbers;
-(int) randomNumber:(int)upperNumber {
return arc4random_uniform(upperNumber);
}
-(NSMutableArray*) lotteryNumbers :(int)withMaximumDrawnNumbers :(int)andHighestNumber {
for (int i = 1; i <= withMaximumDrawnNumbers; i++)
{
int drawnNumber = [self randomNumber:andHighestNumber];
if ([drawnNumbers containsObject:drawnNumber]) { //faulty line
//foo
}
}
return drawnNumbers;
}
@end
回答by manecosta
NSArrays can only contain objective-c objects. So actually the method containsObject:is expecting an object, not an int or any other primitive type.
NSArrays 只能包含objective-c 对象。所以实际上方法containsObject:期待一个对象,而不是 int 或任何其他原始类型。
If you want to store number inside an NSArray you should pack them into NSNumberobjects.
如果您想将数字存储在 NSArray 中,您应该将它们打包到NSNumber对象中。
NSNumber *someNumber = [NSNumber numberWithInt:3];
In your case, if we assume that drawnNumbers is already an array of NSNumbers, you should change the randomNumber:generation to:
在您的情况下,如果我们假设 drawNumbers 已经是一个 NSNumbers 数组,您应该将randomNumber:generation更改为:
-(NSNumber*) randomNumber:(int)upperNumber {
return [NSNumber numberWithInt:arc4random_uniform(upperNumber)];
}
And then when picking it up on the lotteryNumbers method, you should:
然后在使用 lotteryNumbers 方法时,您应该:
NSNumber *drawnNumber = [self randomNumber:andHighestNumber];
Another note would go for the method you defined for lotteryNumbers. You used a really strange name for it, I think you misunderstood how the method naming works in objective-c. You were probably looking for something more like:
另一个注释适用于您为 lotteryNumbers 定义的方法。您为它使用了一个非常奇怪的名称,我认为您误解了方法命名在objective-c 中的工作原理。您可能正在寻找更像的东西:
-(NSMutableArray*) lotteryNumbersWithMaximumDrawnNumbers:(int)maximumDrawnNumbers andHighestNumber:(int)highestNumber;
Late edit:
后期编辑:
Objective-C now allows a way more compact syntax for creating NSNumbers. You can do it like:
Objective-C 现在允许使用更紧凑的语法来创建 NSNumbers。你可以这样做:
NSNumber *someNumber = @(3);
And your method could be rewritten as:
你的方法可以改写为:
-(NSNumber*) randomNumber:(int)upperNumber {
return @(arc4random_uniform(upperNumber));
}
回答by Wain
You are using an int
where an object (presumably NSNumber
) is expected. So convert before use:
您正在使用一个int
对象(大概是NSNumber
)预期的地方。所以在使用前转换:
if ([drawnNumbers containsObject:@( drawnNumber )])