Oracle Oracle
Oracle Oracle

Reputation: 93

NSMutablearray define init issue

i have the following line of code

NSMutableArray *marray = [[NSArray arrayWithObjects: @"4", @"1", @"9", nil]mutableCopy];

and i want to replace it with the following line

NSMutableArray  *marray = [[NSMutableArray alloc]initWithArray:garr];

where garr is global array from global method

the problem is that the code works fine when calling the first line but when using the second one the code crash , appreciate ur help and ideas thanks , iknow that the first one is NSArray but the garr variable source is NSMutable array here is the code for garr

     garr = [[NSMutableArray alloc]init];
    for (int x = 0; x < 10; x++) {
        [garr addObject:[NSNumber numberWithInt: arc4random()%200]];

here is the error msg console error:2012-09-02 14:46:42.976 sort_alg[1561:207] -[NSCFNumber UTF8String]: unrecognized selector sent to instance 0x4b1a170 2012-09-02 14:46:42.978 sort_alg[1561:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber UTF8String]: unrecognized selector sent to instance 0x4b1a170' * Call stack at first throw: –

this is the code that generates the end value

NSString *element; 
NSEnumerator *iterator = [marray objectEnumerator];
while ((element = [iterator nextObject]) != nil)
printf("%s ", [element UTF8String]);
printf("\n");
[marray release];   // array needs to be released! 
[pool release];

thanks

Upvotes: 0

Views: 481

Answers (1)

Cyrille
Cyrille

Reputation: 25144

Problem lies in printf("%s ", [element UTF8String]);.

NSNumber has no UTF8String method, only a stringValue. You can't printf it either, but you can NSLog("%@", [element stringValue]), or NSLog("%d", [element intValue]) if you know it's an int.

Upvotes: 2

Related Questions