數據持久化之plist文件-Bundle目錄及沙箱目錄的自我理解

  • Bundle目錄:就是相當於資源目錄,假設直接放一個plist文件在XCODE中,它就存在於Bundle目錄中,相當於將XCODE所有文件打包後的目錄
  • 沙箱目錄:就是安裝程序後在系統中的目錄,如果不在代碼中手動將plist文件放入沙箱Documents目錄中的話,是不會存在plist文件的!!

下面代碼為將一個plist文件放入到沙箱Documents目錄中

//找到沙箱的Documents目錄
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, TRUE) lastObject];
//拼接目的目錄
    NSString *toPath = [documentDirectory stringByAppendingPathComponent:@"test.plist"];
------
//找到**現在XCODE中plist文件**所在目錄,XXX為與plist在同一級的類名
NSBundle *frameworkBundle = [NSBundle bundleForClass:[XXX class]];
NSString *frameworkBundlePath = [frameworkBundle resourcePath];
//拼接目錄
NSString *fromPath = [frameworkBundlePath stringByAppendingPathComponent:@"test.plist"];
---------
BOOL success = [fileManager copyItemAtPath:fromPath toPath:toPath error:&error];
 if (error) {
            NSAssert(success, @"寫入文件錯誤");
}

你可能感兴趣的:(數據持久化之plist文件-Bundle目錄及沙箱目錄的自我理解)