簡體   English   中英

如何將plist從捆綁包移動到iOS上的文檔文件夾

[英]How to move a plist from the bundle to the documents folder on iOS

所以我想知道的是如何在應用程序第一次運行時將plist從bundle移動到documents文件夾,因為我需要它。 請我找不到怎么做,所以幫助我。

如果你的名字是“朋友”

只需使用下面的代碼(它將查找文件是否存在並復制)

NSFileManager *fileManger=[NSFileManager defaultManager];
NSError *error;
NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];

NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"friends.plist"];

NSLog(@"plist path %@",destinationPath);    
if ([fileManger fileExistsAtPath:destinationPath]){
    //NSLog(@"database localtion %@",destinationPath);
    return;
}
NSString *sourcePath=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"friends.plist:];

[fileManger copyItemAtPath:sourcePath toPath:destinationPath error:&error];

這會將plist從資源復制到文檔目錄

這個功能應該可以解決問題。

- (void)copyPlist {
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"myplist" ofType:@"plist"];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    [[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil];
}

to use the NSError argument in the [[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil]; 你並不使用NSError參數在[[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil]; line,但它可能對調試很有用。

只是一個抬頭......這來自http://samsoff.es/posts/iphone-plist-tutorial “自從JSON解析器在速度上走得很遠以來,使用JSON比Plists更受歡迎。它們實際上更快現在比二元plists(這是堅果)。無論如何,請,請不要使用plist作為你的API。生成它們是緩慢且不可靠的。你應該使用JSON。期間。“

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM