2012-04-27 6 views
3

私が知りたいのは、アプリケーションが初めて実行されたときにバンドルからドキュメントフォルダに移動する方法です。私はそれを行う方法を見つけることができないので、私を助けてください。バンドルからiOS上のドキュメントフォルダにplistを移動する方法

+0

ここで回答します:http://stackoverflow.com/questions/9830061/copy-folder-w-contents-from-bundle-to-documents-directory-ios – XCool

答えて

4

あなたのplist名はドキュメントディレクトリ

にリソースからのplistをコピーするだけで、コードの下に使用

「友人」(それはファイルが存在するかどうかを見つけるかとコピーされます)

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]; 

ある場合

+0

うん、それは動作します! – adr

0

これはhttp://samsoff.es/posts/iphone-plist-tutorialから来ました。JSONパーサーは、実際にはバイナリplist(ナット)よりも高速です。とにかく、あなたのAPIにplistを使わないでください。それらを生成するのが遅く、信頼性が低いです。JSONを使うべきです。

+1

利便性がスピードより重要な場合はどうなりますか? Xcodeの組み込みツールを使用すると、JSONファイルを手動で編集するより楽しく信頼性が高くなります。 plistのタイプセーフティが重要な要素の場合はどうでしょうか? – Till

+0

まで1 - Sean 0 – adr

+1

私はその投稿を信じていません。 BINARY plistは、JASONのようなASCIIベースのフォーマットよりもコンパクトであるため、ネットワーク上をより高速に転送します。値はバイナリ形式で格納されるので、文字列をbool/float/integerなどに変換する必要はありません。文字は区切り文字などを使用しません。私は上級開発者からのプレゼンテーションを見ましたplists、JSON、XMLを比較した多くの人がいました。 JSONはXMLよりも解析速度がやや速く、メモリも少なくて済みましたが、バイナリplistは両方よりも少なくとも10倍高速でした。この偽の投稿がなされたのは約1年前です。 –

2

この機能は、このトリックを行う必要があります。

- (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]; 
} 

あなたは[[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil];ラインにNSError引数を使用するは必要ありませんが、それは、デバッグのために有用である可能性があります。

関連する問題