2017-09-07 8 views
0

プロパティリストを取得する最善の方法は何ですか?メインバンドルとドキュメントディレクトリのplistファイルへのアクセス

1 - メインバンドルから、すなわち:

NSString *path = [[NSBundle mainBundle] pathForResource:@"recipes" ofType:@"plist"]; 

2 - またはドキュメントディレクトリからのパスを取得する、すなわち:

// Data.plist code - get path from root direcory 
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
// get documents path 
NSString *documentsPath = [paths objectAtIndex:0]; 
// get the path to our plist file. 
NSString *plistPath = [documentsPath 
stringByAppendingPathComponent:@"Data.plist"]; 
// check to see if Data.plist exists in documents 
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { 
    // if not in documents, get property list from main bundle 
    plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; 
} 

何、なぜ? 両方が正しい場合、なぜ1行のコードで最初のものを使用しないのですか?

答えて

2

単純なルール:

  • をプロパティリストファイルは読み取り専用バンドルにそれを置くをある場合。
  • プロパティリストファイルがの場合は、をドキュメントディレクトリに置きます。
+1

さらに重要なことに、アプリケーションバンドルのファイルは、アプリケーションの一部としてパッケージ化されています。ドキュメントフォルダ内のファイルはランタイムイベントです。ファイルはアプリの一部として開始されません。 – rmaddy

+0

あなたの素早い対応のために@vadianに感謝します。 –

関連する問題