NSKeyedArchiver archiveRootObject keeps returning NO

前端 未结 2 1700
旧时难觅i
旧时难觅i 2021-01-14 12:24

I\'ve spent the past few hours trying to figure this out, but I\'ve ran out of ideas.

All I\'m trying to do is archive an object, but the method archiveRootObj

相关标签:
2条回答
  • 2021-01-14 12:57

    You create a directory with the path fullPath and then you try to write the file at the same path. Overwriting a directory with a file like this is not possible. Use your cacheDirectory string to create your directory.

    0 讨论(0)
  • 2021-01-14 12:59

    NSString *cacheDirectory is not a mutable string and after you init it, you attempt to modify it so you are writing to the top-level directory.

    For a quick fix, try:

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,   NSUserDomainMask, YES);
    NSString *documentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil;
    NSString *documentsResourcesPath = [documentPath  stringByAppendingPathComponent:@"MyAppCache"];
    
    NSString *fullPath = [documentsResourcesPath stringByAppendingPathComponent:@"archive.data"];
    
    0 讨论(0)
提交回复
热议问题