Where'd my sounds go?
In my Row class I have the initWithCoder method and everything restored in that method works fine, but only within the method. After the method is called I loose an array of sounds that is in my Row class. The sounds class also has the initWithCoder method, and the sound plays fine but only in the Row class initWithCoder method. After decoding the Row object, the sound array disappears completely and is unable to be called.
Here's my source for the initWithCoder:
- (id) initWithCoder:(NSCoder *)coder {
...
soundArray = [coder decodeObjectForKey:@"soundArray"];
NSLog(@"%d",[soundArray count]);
return self;
}
t开发者_如何学运维he log shows the count as 8 like it should (this is while unarchiving). Then the row object I create gets assigned. And the resulting row object no longer has a soundArray.
[NSKeyedArchiver archiveRootObject:row toFile:@"DefaultRow"];
...
row = [NSKeyedUnarchiver unarchiveObjectWithFile:@"DefaultRow"];
So now whenever I call the soundArray it crashes.
//ERROR IS HERE
NSLog(@"%d",[[row soundArray] count]);
Help please (soundArray is an NSMutableArray).
You need to retain soundArray in initWithCoder:
— you can read up on Cocoa memory management on Apple's site.
If you don't retain
soundArray, it will later be dealloc
'ed since you haven't claimed ownership of it. Basically, decodeObjectForKey:
returns an object that has been autoreleased
.
精彩评论