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

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

问题描述

我正在尝试序列化实现 NSCoding 协议的 NSObjects 的两个 NSMutableArrays .但是,它适用于一个(纸牌),而不适用于其他(纸牌).我有以下代码块:

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 NO ,而 BOOL s YES . stacks 数组实际上是空的(这可能是它起作用的原因). cards 数组包含内容.为什么包含内容的数组出现故障?我不知道这一点.我遍历了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...).

请参阅:您想要的是 NSArchiver ,更具体地说,是阅读

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

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

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