Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...