NSArray writeToFile 失败

发布于 2024-09-05 04:55:36 字数 1087 浏览 2 评论 0原文

我试图将一个数组(其中包含一些字典)保存到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把人绕傻吧 2024-09-12 04:55:36

如果您阅读文档 紧密相连,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 NSArrays. In particular, the value [NSNull null] is not acceptable.

蓝天白云 2024-09-12 04:55:36

我在序列化之前将 NSArray 或 NSDictionary 转换为 NSData。以下是 nsarray 上用于序列化和反序列化的类别。这个舒适的处理一些数据是 nsnull

@implementation NSArray(Plist)

-(BOOL)writeToPlistFile:(NSString*)filename{
    NSData * data = [NSKeyedArchiver archivedDataWithRootObject:self];
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentsDirectory = [paths objectAtIndex:0];
    NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
    BOOL didWriteSuccessfull = [data writeToFile:path atomically:YES];
    return didWriteSuccessfull;
}

+(NSArray*)readFromPlistFile:(NSString*)filename{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentsDirectory = [paths objectAtIndex:0];
    NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
    NSData * data = [NSData dataWithContentsOfFile:path];
    return  [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

@end //needs to be set for implementation

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

@implementation NSArray(Plist)

-(BOOL)writeToPlistFile:(NSString*)filename{
    NSData * data = [NSKeyedArchiver archivedDataWithRootObject:self];
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentsDirectory = [paths objectAtIndex:0];
    NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
    BOOL didWriteSuccessfull = [data writeToFile:path atomically:YES];
    return didWriteSuccessfull;
}

+(NSArray*)readFromPlistFile:(NSString*)filename{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentsDirectory = [paths objectAtIndex:0];
    NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
    NSData * data = [NSData dataWithContentsOfFile:path];
    return  [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

@end //needs to be set for implementation
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文