Bharat
Bharat

Reputation: 3007

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?

Upvotes: 1

Views: 96

Answers (3)

Nick Moore
Nick Moore

Reputation: 15847

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

Upvotes: 1

justin
justin

Reputation: 104698

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

Upvotes: 0

Johannes Fahrenkrug
Johannes Fahrenkrug

Reputation: 44700

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

Johannes.

Upvotes: 3

Related Questions