-writeToURL:atomically:
anURLで指定したURLに書き込みます
-(BOOL)writeToURL:(NSURL *)sender atomically:(BOOL)atomically
解説
anURLで指定したURLに書き込みます。
flagがYESなら、元ファイルを残し、書き終わってからリネームします。
返り値
( BOOL )
書き込めたかYES/NO
引数
( NSURL * )sender
URL
( BOOL )atomically
安全に書き込むかYES/NO
フレームワーク
Foundation
クラス
NSArray
使用可能
10.0
iOS2.0
例文
//バンドルに書き込めなくなったので、ドキュメントに書き込み //http://cocoaapi.hatenablog.com/entry/00040417/NSArray_writeToURL_atomically_ -(void)method011 { //書き込み用のNSArrayを作成 NSArray *anArray = @[@"aaa",@2.0f,@"bbb",@"ccc"]; //メインバンドル内のResourceにplistfile2.plistファイルを作成 NSFileManager *myFile = [ NSFileManager defaultManager]; NSDictionary *dic = @{NSFileModificationDate: [NSDate date], @"NSFileOwnerAccountName": @"owner", @"NSFileGroupOwnerAccountName": @"group", @"NSFilePosixPermissions":@0777 , @"NSFileExtensionHidden": @YES}; NSMutableData *dat1 = [[NSMutableData alloc] initWithCapacity:1]; //ドキュメントディレクトリに NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; [myFile changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]]; [myFile createFileAtPath:@"plistfile2.plist" contents:dat1 attributes:dic]; NSLog(@"011 myFile path = %@",[myFile currentDirectoryPath]); NSString *resourcePath = [documentsDirectory stringByAppendingString:@"/plistfile2.plist"]; NSLog(@"************************* = %@",resourcePath); NSURL *url = [NSURL fileURLWithPath:resourcePath]; //書き込み [anArray writeToURL:url atomically:YES]; //ファイルから読み込んで、NSArrayを作成 NSArray *readArray = [NSArray arrayWithContentsOfFile:resourcePath]; NSLog(@"011 = %@",[readArray description]); //=>(aaa,2,bbb,ccc) }