macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

initWithContentsOfURL:encoding:error:

INDEX>Foundation>NSString>

指定したファイルの内容を指定したエンコーディングで初期化して返します
-(id)initWithContentsOfURL:(NSURL *)url
          encoding:(NSStringEncoding)enc
          error:(NSError **)error

解説

指定したファイルの内容を指定したエンコーディングで初期化して返します。
10.4以降はstringWithContentsOfFile:を使わず、このメソッドを使います。

返り値

( id )

なし

引数

( NSURL * )url

URL

( NSStringEncoding )enc

エンコード

( NSError ** )error

エラーを返すエラーオブジェクト

クラス

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