views:

19

answers:

1

So I stored an NSMutableArray of NSNumber objects into the file "times.plist" then I load it and retain it on launch and the NSLog shows the correct value, but later the [times count] equals 0. Why are the NSNumbers disappearing?

times = [[NSMutableArray alloc] initWithObjects:[[NSNumber alloc] initWithFloat:time],nil];
...

[times writeToFile:@"times.plist" atomically:YES];
...

times = [[NSMutableArray arrayWithContentsOfFile:@"times.plist"] retain];
NSLog(@"%f",[[times objectAtIndex:0] floatValue]);
A: 

Okay DUMB DUMB DUMB. Never do what I did. in my -init; method i had already initiated my times array with the code:

times = [[NSMutableArray alloc] initWithObjects:nil];

and I was trying to restore them with:

times = [[NSMutableArray arrayWithContentsOfFile:@"times.plist"] retain];

BUT I was restoring it BEFORE I was initiating it and that erased it every time. Learn from my mistake and check the order of your -init; methods. ALWAYS.

Dane Graphics
You shouldn't even need to initialize it using `initWithObjects` if you're going to use `arrayWithContentsOfFile`.
Ben Alpert