iPhone / Objective-C:NSMutableArray writeToFile不会写入文件。始终返回NO [英] iPhone / Objective-C: NSMutableArray writeToFile won't write to file. Always returns NO

查看:259
本文介绍了iPhone / Objective-C:NSMutableArray writeToFile不会写入文件。始终返回NO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试序列化实现 NSCoding <的 NSObjects 的两个 NSMutableArrays / code>协议。但是它适用于一个(堆栈)而不适用于其他()。我有以下代码块:

I'm trying to serialize two NSMutableArrays of NSObjects that implement the NSCoding protocol. However it works for one (stacks) and not the other (cards). I have the following block of 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];
}

我使用调试器逐步完成此方法,并在最后两行之后代码运行,我检查两个 BOOL 的值。 BOOL c BOOL s stacks 数组实际上是空的(这可能就是它的工作原理)。 数组包含内容。为什么包含内容的数组失败了?我无法弄清楚这一点。我查看了SOF上的众多帖子,他们每个人都说这个问题是因为他们写的文件的保护级别阻止了他们写作。这不是我的问题,因为我正在写入Documents文件夹。我已经检查过双重和三重检查, rootStack.cards rootStack.stacks 都不是。我已经检查过卡片确实有内容。

I step through this method using the debugger, and after the last two lines of code run, I check the values of the two BOOLs. BOOL c is NO and BOOL s is YES. The stacks array is actually empty (which is probably why it works). The cards array has contents. Why is it that the array with contents is failing? I can't figure this out. I've looked through numerous threads on SOF, each of them say the problem is because the protection level of the files they were writing were preventing them from writing. This is not my problem, as I'm writing to the Documents folder. I've double and tripple checked that neither rootStack.cards nor rootStack.stacks is nil. And I've checked that cards does indeed have content.

以下是我的Notecard类的编码器方法(我添加了所有 if 作为尝试解决此问题以确保尝试编码nil值不会破坏某些内容的一部分的语句:

Here are the coder methods for my Notecard class (I added all the if statments as part of trying to solve this problem to make sure trying to encode nil values doesn'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;
}

每个字段都是NSString,NSNumber或UIColor。

each field is either an NSString, NSNumber, or UIColor.

感谢您提供任何帮助

推荐答案

您将资产列表与存档混淆。这很容易做到。

You are confusing property lists with archiving. It is easy to do.

writeToFile:atomically:要求内容符合属性列表;也就是说,数组中的所有对象都是属性列表中允许的类之一的实例(字典,字符串,数据,数组等等)。

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

请参阅: writeToFile:atomically:'s documentation

你想要的是 NSArchiver ,更具体地说,阅读存档指南

What you want is NSArchiver and, more specifically, to read the archiving guide.

这篇关于iPhone / Objective-C:NSMutableArray writeToFile不会写入文件。始终返回NO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆