本文介绍了比较目标C - ARC不允许'int'到'id'的隐式转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的错误隐式转换'int'到'id'不允许与ARC标记为faulty line的行。我想它有一些与我在检查数组中的整数,包含对象而不是整数。有关。

  #importRandomGenerator.h

@implementation RandomGenerator

NSMutableArray * drawnNumbers;

- (int)randomNumber:(int)upperNumber {
return arc4random_uniform(upperNumber);
}

- (NSMutableArray *)lotteryNumbers:(int)withMaximumDrawnNumbers:(int)andHighestNumber {
for(int i = 1; i {
int drawnNumber = [self randomNumber:andHighestNumber];
if([drawnNumbers containsObject:drawnNumber]){//错误的行
// foo
}
}
return drawnNumbers;
}

@end


解决方案>

NSArrays只能包含目标c对象。所以实际上方法 containsObject:期望一个对象,而不是一个int或任何其他原始类型。



如果你想在一个NSArray您应该将它们打包到 NSNumber 对象中。

  NSNumber * someNumber = [NSNumber numberWithInt:3] ; 

在你的情况下,如果我们假设drawnNumbers已经是一个NSNumbers数组, strong> randomNumber:代为:

   - (NSNumber *)randomNumber:(int)upperNumber {
return [NSNumber numberWithInt:arc4random_uniform(upperNumber)];
}

然后当在lotteryNumbers方法上使用它时, p>

  NSNumber * drawnNumber = [self randomNumber:andHighestNumber]; 

另一个注释将针对您为lotteryNumbers定义的方法。你使用了一个非常奇怪的名字,我认为你误解了方法命名在objective-c中是如何工作的。你可能在寻找更像:

   - (NSMutableArray *)lotteryNumbersWithMaximumDrawnNumbers:(int)maximumDrawnNumbers andHighestNumber:(int)highestNumber ; 

晚期编辑


$ b b

Objective-C现在允许一种更紧凑的语法来创建NSNumbers。你可以这样做:

  NSNumber * someNumber = @(3);您的方法可以重写为:


- (NSNumber *)randomNumber:(int)upperNumber {
return @(arc4random_uniform(upperNumber));
}


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.

#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
解决方案

NSArrays can only contain objective-c objects. So actually the method containsObject: is expecting an object, not an int or any other primitive type.

If you want to store number inside an NSArray you should pack them into NSNumber objects.

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:

-(NSNumber*) randomNumber:(int)upperNumber {
    return [NSNumber numberWithInt:arc4random_uniform(upperNumber)];
}

And then when picking it up on the lotteryNumbers method, you should:

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:

-(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:

NSNumber *someNumber = @(3);

And your method could be rewritten as:

-(NSNumber*) randomNumber:(int)upperNumber {
    return @(arc4random_uniform(upperNumber));
}

这篇关于比较目标C - ARC不允许'int'到'id'的隐式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:06