initWithContentsOfFile:encoding:error:
指定したファイルの内容から指定したエンコーディングで読み込んだ文字列を、初期化して返します。
ファイルが読めなかった場合、nilが返され、errorにエラーの内容を示すNSErrorオブジェクトが返されます。
-(id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error
解説
指定したファイルの内容から指定したエンコーディングで初期化して返します。
10.4以降はstringWithContentsOfFile:を使わず、このメソッドを使います。
返り値
( id )
オブジェクト()
引数
( NSString * )path
ファイルパス
( NSStringEncoding )enc
( NSError ** )error
エラーを返すエラーオブジェクト、error情報が不要な場合はNULLを渡します。
フレームワーク
Foundation
クラス
NSString
Instance Methods
使用可能
10.4
参照
+stringWithContentsOfFile:encoding:error:
-initWithContentsOfFile:usedEncoding:error:
例文
#pragma mark stringWithContentsOfFile:encoding:error: -(void)method018 { NSBundle *bundle = [NSBundle mainBundle ]; NSString *resourcePath = [bundle resourcePath]; NSString *filePath1 = [resourcePath stringByAppendingPathComponent:@"Textfile.txt"]; NSLog(@"%s : %@",__FUNCTION__ ,resourcePath); NSError *error; NSString *output1 = [[NSString alloc] initWithContentsOfFile:filePath1 encoding: NSASCIIStringEncoding error: &error]; NSLog(@"%s %p , %@",__FUNCTION__,output1,output1); //Textfile.txtの内容はSample Text //=>-[OOOAppDelegate method018] 0x158940 , Sample Text NSString *filePath2 = [resourcePath stringByAppendingPathComponent:@"not exist.txt"]; NSString *output2 = [[NSString alloc] initWithContentsOfFile:filePath2 encoding: NSASCIIStringEncoding error: &error]; NSLog(@"%s %p , %@",__FUNCTION__,output2,output2); NSLog(@"%s %p , %@",__FUNCTION__,error,[error localizedDescription]); //not exist.txtというファイルは存在しないのでエラーになる //=>-[OOOAppDelegate method018] 0x0 , (null) //=>-[OOOAppDelegate method018] 0x1690b0 , The operation couldn窶冲 be completed. (Cocoa error 260.) //error情報が要らなければerrorにNULLを渡す NSString *filePath3 = [resourcePath stringByAppendingPathComponent:@"not exist.txt"]; NSString *output3 = [[NSString alloc] initWithContentsOfFile:filePath3 encoding: NSASCIIStringEncoding error: NULL]; NSLog(@"%s %p , %@",__FUNCTION__,output3,output3); }
#pragma mark stringWithContentsOfFile:encoding:error: - (void)displayText030:(NSString *)text { self.fileString = text; NSLog(@"text %@",text); } -(void)method030 { //FileSystem programming guide //http://developer.apple.com/library/ios/#DOCUMENTATION/FileManagement/Conceptual/FileSystemProgrammingGUide/UsingtheOpenandSavePanels/UsingtheOpenandSavePanels.html //OpenPanelを作る NSOpenPanel *opPanel = [ NSOpenPanel openPanel ]; //OpenPanelでファイル選択 //This way is 10.6 or later [opPanel setCanChooseDirectories:YES]; [opPanel setAllowsMultipleSelection:YES]; [opPanel setPrompt:NSLocalizedString(@"Select", nil)]; [opPanel setMessage:@"Please select ASCII text."]; [opPanel beginWithCompletionHandler:^(NSInteger result){ if (result == NSFileHandlingPanelOKButton) { NSURL* theDoc = [[opPanel URLs] objectAtIndex:0]; // Open the document. NSError *error = nil; NSString *output = [[NSString alloc] initWithContentsOfFile:[theDoc path] encoding: NSASCIIStringEncoding error: &error]; [self performSelectorOnMainThread:@selector(displayText030:) withObject:output waitUntilDone:NO]; } }]; }