开发者

Help needed to assign the value of an object of array to a variable

开发者 https://www.devze.com 2023-02-08 11:02 出处:网络
int random=0; int counter=0; while(counter<25) { ra开发者_开发知识库ndom=arc4random() % 40; BOOL flag=[array containsObject:[NSNumber numberWithInt:random] ];
int random=0;
int counter=0;
while(counter<25)
{   
    ra开发者_开发知识库ndom=arc4random() % 40;
    BOOL flag=[array containsObject:[NSNumber numberWithInt:random] ];
    if(flag)
    {   
        counter--;
    }
    else
    {       
        [array addObject:[NSNumber numberWithInt:random]];
        int p=[array objectAtIndex:counter];    //**line4
        counter++;
    }   
}

getting a warning on line 4, not able to assign the value of an object of an array to a variable, please help me Thanks


-objectAtIndex: returns an object — in your case, an instance of NSNumber. To convert it to int, use the -intValue method. For instance, replace

int p=[array objectAtIndex:counter];

with

int p=[[array objectAtIndex:counter] intValue];


You have to do Type casting Nsnumber to int.For converting you can use IntValue metod.for knowing more about Foundation Data Type use apple s documents.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号