简体   繁体   中英

objective-c fetching array elements?

this line of code gives the ans. 2 (which is right)

int def = arc4random() % 2;
NSLog(@"%@",[uniqueNumbers objectAtIndex:def]);

but when i use

int def = arc4random() % 2;
//NSLog(@"%@",[uniqueNumbers objectAtIndex:def]);
c1 = (int)[uniqueNumbers objectAtIndex:def];
NSLog(@"%d", c1);

it gives 115669616. where c1 is an integer. what is the problem?

You get an NSNumber instance back. You need to unbox it by calling intValue on it, then you'll be good to go.

Johannes.

Try c1 = [[uniqueNumbers objectAtIndex:def] intValue]; .

NSArray holds objects , not integers. that cast to int is not valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM