NSArray writeToFile失败 [英] NSArray writeToFile fails

查看:229
本文介绍了NSArray writeToFile失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个内置字典的数组保存到plist文件中,但它失败了。我没有任何错误。我在代码中使用另一个数组执行完全相同的几行,并且可以工作..我无法弄清楚为什么它不保存文件。

I am trying to save an array, which has some dictionaries inside, to a plist file but it fails. I don't get any errors. I do exactly the same few lines above in the code just with another array and that works.. I can't figure out why it does not save the file.

这是我保存文件的地方:(参见下面的一些调试器输出)

This is where I save the file: (see some debugger output below)

// When built parse through dictionary and save to file
    for ( NSString *keys in [dicByCountry allKeys] )
    {
        NSArray *arrr = [[NSArray alloc] initWithArray:[dicByCountry objectForKey:keys]];
        NSString *fname = [self filePath:[NSString stringWithFormat:@"regions.cid%@.plist",keys]];
        if (![arrr writeToFile:fname atomically:YES])
            NSLog(@"Could not write file regions.cid%@.plist",keys);
    }

这里有一些GDB输出

(gdb) po fname
/Users/chris/Library/Application Support/iPhone Simulator/4.0/Applications/44A9FF9E-5715-4BF0-9BE2-525883281420/Documents/regions.cid0.plist


(gdb) po arrr
<__NSArrayI 0x8022b30>(
{
    countryID = "<null>";
    region = "?\U00e2vora";
    regionID = 16;
},
{
    countryID = "<null>";
    region = Vicenza;
    regionID = 14;
},
{
    countryID = "<null>";
    region = Wales;
    regionID = 23;
}
)


推荐答案

如果您阅读文档密切关注, writeToFile:atomically: expect t他的数组只包含可以写入plist文件的对象。

If you read the documentation closely, writeToFile:atomically: expects the array to contain only objects which can be written into a plist file.

只有以下类型的对象:


  • NSString

  • NSData

  • NSDate

  • NSNumber

  • NSArray

  • NSDictionary

  • NSString
  • NSData
  • NSDate
  • NSNumber
  • NSArray
  • NSDictionary

是允许的。如果你要保存的数组中有数组或字典,它们的值将按相同的标准进行检查。

are permitted. If you have arrays or dictionaries within the array you're saving, their values will be examined by the same criteria.

这比通常允许的更严格一些。 code>的NSArray 秒。特别是,值 [NSNull null] 是不可接受的。

This is somewhat more restrictive than what's usually allowed in NSArrays. In particular, the value [NSNull null] is not acceptable.

这篇关于NSArray writeToFile失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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