IPhone / Objective-C: NSMutableArray writeToFile will not write to the file. Always returns NO

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

+4
source share
2 answers

You confuse property lists with archiving. It is easy to do.

writeToFile:atomically: requires the content to match the property list; that is, all objects in the array will be an instance of one of the classes allowed in property lists (dictionaries, strings, data, arrays, etc.).

See: writeToFile: atomically: documentation .

You want NSArchiver and, more specifically, read the archiving guide .

+14
source

The writeToFile:atomically: method in the NSMutableArray that you call only works if all the elements in the array are objects in the property list ( NSString , NSData , NSArray or NSDictionary ). does NSPropertyListSerialization . This is not the same as key archiving, which is done using NSKeyedArchiver and the NSCoding protocol. See the Cocoa Archives and Serialization Guide for more information.

The specific problem is that UIColor not a property list object. However, it runs the NSCoding protocol and should work in the archive. Try something like

 BOOL c = [NSKeyedArchiver archiveRootObject:rootStack.card toFile:cardsFile]; BOOL s = [NSKeyedArchiver archiveRootObject:rootStack.stacks toFile:stacksFile]; 
+3
source

Source: https://habr.com/ru/post/1308798/


All Articles