Lefora Free Forum
login join
Loading
844 views

Can't get NSMutableArray to work.

Page 1
1–2
rookie - member
1 posts

I can only get NSMutable array to add NSStrings, but when I try something like this:

NSMutableArray *array = [NSMutableArray array];
NSNumber *number = [[NSNumber alloc] initWithUnsignedInt:5];
[array addObject:number];

I get the following error:

*** -[NSCFNumber length]: unrecognized selector sent to instance 0x107f90

I've tried it with other objects as well, and NSString seems to be the only one that works.  Any reason why?


?
288 posts


Hi T

I know it's been a while, however better late than never!   I didn't have any difficulty with this using XCode 3.2.1 on Snow Leopard.
#import <Cocoa/Cocoa.h>

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSMutableArray *array = [NSMutableArray array];

NSString* a = [[NSString alloc] initWithCString:"who"] ;
NSString* b = [[NSString alloc] initWithCString:"flung"] ;
NSNumber* n = [[NSNumber alloc] initWithUnsignedInt:2];
NSString* d = [[NSString alloc] initWithCString:"dung"] ;
[array addObject:a];
[array addObject:b];
[array addObject:n];
[array addObject:d];

for ( size_t i = 0 ; i < [array count] ; i++ ) {
NSLog(@"%d : %@",i,[array objectAtIndex:i]) ;
}

    [pool drain];
    return 0 ;
}

Output:

506 /Users/rmills/Projects/WeeTest2/build/Debug/WeeTest2.app/Contents/MacOS> ./WeeTest2 
2010-01-15 21:24:38.171 WeeTest2[3851:903] 0 : who
2010-01-15 21:24:38.173 WeeTest2[3851:903] 1 : flung
2010-01-15 21:24:38.174 WeeTest2[3851:903] 2 : 2
2010-01-15 21:24:38.174 WeeTest2[3851:903] 3 : dung
507 /Users/rmills/Projects/WeeTest2/build/Debug/WeeTest2.app/Contents/MacOS> 

__________________
Page 1
1–2

Locked Topic


You must be a member to post in this forum

Join Now!