Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

ios - Incompatible integer to pointer conversion initializing NSArray *

I am running the code like this and it is returning me this error:

Incompatible integer to pointer conversion initializing NSArray *_strong with an expression of type 'int'.

The code is like this:

- (NSArray *)randperm:(int)total
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    int counter = 0;
    while (counter < total) {
        NSNumber *randomInteger = [NSNumber numberWithInt:(arc4random_uniform(total)+1)];
        if (![array containsObject:randomInteger]) {
            [array addObject:(randomInteger)];
            counter++;
        }

    }
    NSArray *arr = [array copy];
    return arr;
}

I am calling it this:

NSArray *array = randperm(6);

This line is returning me an error. Not sure why there is a such an error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are calling the Objective-C method as if it was C:

NSArray *array = randperm(6);

It should be:

NSArray *array = [self randperm:6];

Also there is no need to make a copy of the array before returning it:

NSArray *arr = [array copy];
return arr;

Just do:

return array;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...