I am trying to serialize two NSMutableArrays from NSObjects that implement the NSCoding protocol. However, it works for one ( stacks ) and not the other ( cards ). I have the following code:
-(void) saveCards { NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* documentsDirectory = [paths objectAtIndex:0]; NSString* cardsFile = [documentsDirectory stringByAppendingPathComponent:@"cards.state"]; NSString* stacksFile = [documentsDirectory stringByAppendingPathComponent:@"stacks.state"]; BOOL c = [rootStack.cards writeToFile:cardsFile atomically:YES]; BOOL s = [rootStack.stacks writeToFile:stacksFile atomically:YES]; }
I go through this method with a debugger, and after the last two lines of code, I check the values โโof two BOOL s. BOOL c NO , and BOOL s YES . The stacks array stacks actually empty (it probably works). The cards array has content. Why does the content array not work? I canโt figure it out. I looked at many threads in SOF, each of them says that the problem is that the level of protection of the files they write prevented them from writing. This is not my problem as I am writing to the Documents folder. I double-checked and triple that neither rootStack.cards nor rootStack.stacks are there. And I checked that the maps do have content.
Here are the encoder methods for my Notecard class (I added all if statuses as part of trying to solve this problem, to make sure that trying to encode nil values โโwon't break something):
-(void) encodeWithCoder:(NSCoder *)encoder { if(text) [encoder encodeObject:text forKey:@"text"]; if(backText) [encoder encodeObject:backText forKey:@"backText"]; if(x) [encoder encodeObject:x forKey:@"x"]; if(y) [encoder encodeObject:y forKey:@"y"]; if(width) [encoder encodeObject:width forKey:@"width"]; if(height) [encoder encodeObject:height forKey:@"height"]; if(timeCreated) [encoder encodeObject:timeCreated forKey:@"timeCreated"]; if(audioManagerTicket) [encoder encodeObject:audioManagerTicket forKey:@"audioManagerTicket"]; if(backgroundColor) [encoder encodeObject:backgroundColor forKey:@"backgroundColor"]; } -(id) initWithCoder:(NSCoder *)decoder { self = [super init]; if(!self) return nil; self.text = [decoder decodeObjectForKey:@"text"]; self.backText = [decoder decodeObjectForKey:@"backText"]; self.x = [decoder decodeObjectForKey:@"x"]; self.y = [decoder decodeObjectForKey:@"y"]; self.width = [decoder decodeObjectForKey:@"width"]; self.height = [decoder decodeObjectForKey:@"height"]; self.timeCreated = [decoder decodeObjectForKey:@"timeCreated"]; self.audioManagerTicket = [decoder decodeObjectForKey:@"audioManagerTicket"]; self.backgroundColor = [decoder decodeObjectForKey:@"backgroundColor"]; return self; }
each field is either NSString, NSNumber, or UIColor.
Thanks for any help
source share