简体   繁体   中英

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

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.

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