initWithContentsOfURL:encoding:error:
指定したファイルの内容を指定したエンコーディングで初期化して返します
-(id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error
解説
指定したファイルの内容を指定したエンコーディングで初期化して返します。
10.4以降はstringWithContentsOfFile:を使わず、このメソッドを使います。
返り値
( id )
なし
フレームワーク
Foundation
クラス
NSString
Instance Methods
使用可能
10.4
参照
+stringWithContentsOfURL:encoding:error:
例文
#pragma mark initWithContentsOfURL:encoding:error: - (void)displayText034:(NSString *)text { self.fileString = text; NSLog(@"text %@",text); } -(void)method034 { //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(@"prompt text", nil)]; [opPanel setMessage:@"Message"]; [opPanel beginWithCompletionHandler:^(NSInteger result){ if (result == NSFileHandlingPanelOKButton) { NSURL* theURL = [[opPanel URLs] objectAtIndex:0]; // Open the document. NSError *error = nil; NSString *output = [[NSString alloc] initWithContentsOfURL:theURL encoding: NSASCIIStringEncoding error: &error]; [self performSelectorOnMainThread:@selector(displayText034:) withObject:output waitUntilDone:NO]; } }]; }