0

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

3
  • Error message, crash log, stacktrace, core dump? Commented Sep 2, 2012 at 11:45
  • Your first array contains strings, the second one numbers. It looks like some other code that you're not showing here, expects the array to still contain strings... Commented Sep 2, 2012 at 11:49
  • @OracleOracle: Please update your question, don't paste the error message in the comment. Commented Sep 2, 2012 at 11:49

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

By the way, instead of going all the way to instanciating an enumerator, you can just use fast enumeration: for (NSNumber *num in marray) { NSLog("%@", [num stringValue]); }.
don't use equal '=' for comparison of objets. use isEqual:
Hey, you could printf("%s", [[element stringValue] UTF8String]);
All went fine many thanks the issue is with the NSstring and Nsnumber with thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.