【问题标题】:I have successfully saved my plist to the documents directory! Now I can't load it back我已成功将我的 plist 保存到文档目录中!现在我无法加载它
【发布时间】:2010-11-27 03:43:40
【问题描述】:

这段代码可以很好地保存我的 plist 到文档目录。

- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.addButtonItem;
NSString *path = [[NSBundle mainBundle] pathForResource:@"TitleArray" ofType:@"plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.titles = tmpArray;
[tmpArray release];

//TESTING NEW CODE FOR SAVING TO DOC DIR
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:@"TitleArray.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if(![fileManager fileExistsAtPath: docDirPath])
{
    NSString *bundle = [[NSBundle mainBundle] pathForResource:@"TitleArray" ofType:@"plist"];

    [fileManager copyItemAtPath:bundle toPath:docDirPath error:&error];

                        NSLog(@"plist is copied to Directory");
                        }

我无法弄清楚如何将 plist 加载回应用程序!

有人可以帮忙吗?谢谢。

【问题讨论】:

    标签: objective-c cocoa plist saving-data


    【解决方案1】:
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"TitleArray.plist"];
    NSArray * myList = [NSArray arrayWithContentsOfFile:path];
    NSMutableArray *tmpArray = [myList mutableCopy];
    self.titles = tmpArray;
    [tmpArray release];
    

    没用?

    【讨论】:

    • 当您保存 plist 时,您将其保存在您的文档目录中。当你加载它时,你试图从包中检索它,这不是你保存它的地方。 Nacho 的代码在这里向您展示了如何从您放置它的文档目录中检索它。
    • 感谢马修为安迪添加了这种解释;)
    • 嘿!!!!有用!! nacho4d 谢谢!马修——谢谢你为我拼写出来。我真的很感激。
    【解决方案2】:

    如果您询问如何覆盖包含在应用程序主包中的 plist,那么您应该知道它无法完成。看看这个问题了解更多细节:Can you update a file in the application bundle?

    【讨论】:

    • 嗨,地方病,我并没有试图覆盖 plist(我措辞不当的问题似乎确实在问这个问题)只是想从 doc 目录中检索数据。供应用程序使用。说真的,这样说的正确方式是什么?它是读入/写出还是什么?谢谢。
    【解决方案3】:
    NSError *error; NSArray *paths =    
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths    objectAtIndex:0];
    NSString *docDirPath = [documentsDirectory    stringByAppendingPathComponent:@"TitleArray.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath: docDirPath])  
    {    
        docDirPath = [[NSBundle mainBundle] pathForResource:@"TitleArray" ofType:@"plist"];
    }  
    NSMutableArray *tmpArray = [[[NSMutableArray    alloc] initWithContentsOfFile: docDirPath] mutableCopy];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2014-07-20
      • 2022-08-21
      • 2014-11-17
      • 1970-01-01
      相关资源
      最近更新 更多