NSArray writeToFile 失败
我试图将一个数组(其中包含一些字典)保存到 plist 文件中,但失败了。我没有收到任何错误。我在代码中对上面的几行执行了完全相同的操作,只是使用另一个数组,并且有效。我不明白为什么它不保存文件。
这是我保存文件的位置:(请参阅下面的一些调试器输出)
// 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;
}
)
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);
}
Here some GDB Output
(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;
}
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您阅读文档 紧密相连,
writeToFile:atomically:
期望数组仅包含可以写入 plist 文件的对象。仅限以下类型的对象:
NSString
NSData
NSDate
NSNumber
NSArray
NSDictionary< /code>
是允许的。如果要保存的数组中有数组或字典,则将按照相同的条件检查它们的值。
这比 NSArray 通常允许的限制要严格一些。特别是,值
[NSNull null]
是不可接受的。If you read the documentation closely,
writeToFile:atomically:
expects the array to contain only objects which can be written into a plist file.Only objects of type:
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.
This is somewhat more restrictive than what's usually allowed in
NSArray
s. In particular, the value[NSNull null]
is not acceptable.我在序列化之前将 NSArray 或 NSDictionary 转换为 NSData。以下是 nsarray 上用于序列化和反序列化的类别。这个舒适的处理一些数据是 nsnull
I convert NSArray or NSDictionary to NSData before serializing. Following is a category on nsarray for serializing and deserializing. This comfortableby handles some data being nsnull