initWithContentsOfFile:
ファイルの内容からNSStringを初期化して返します
-(id)initWithContentsOfFile:(NSString *)path
解説
■10.4以降非推奨。
(initWithContentsOfFile:encoding:error: または initWithContentsOfFile:usedEncoding:error: を使います。)
ファイルの内容からNSStringを初期化して返します。
ファイルがU+FEFFまたはU+FFFEから始まるならばユニコード文字と解釈します。
でなければC文字列と解釈します。
ファイルを開くことが出来なければnilを返します。
返り値
( id )
文字列(NSStringまたはそのサブクラス)
引数
( NSString * )path
読み込むファイルのパス
フレームワーク
Foundation
クラス
NSString
Instance Methods
使用可能
10.0
参照
+ stringWithContentsOfFile:
+ defaultCStringEncoding
例文
- (IBAction)set:(id)sender { //開けるファイル拡張子の配列 NSArray *imgTypes = [ NSArray arrayWithObject : @"html" ]; //OpenPanelを作る NSOpenPanel *opPanel = [ NSOpenPanel openPanel ]; //NSString *str; //OpenPanelの結果のボタン番号 int opRet; //OpenPanelでファイル選択 opRet = [ opPanel runModalForDirectory : NSHomeDirectory() //どこのディレクトリを出すか file : @"Pictures" //どのどのファイルを選択しておくか types : imgTypes ];//選べるファイルタイプ if ( opRet == NSOKButton ) { // OPENPanelのボタンがOKなら //NSImageを作ってファイルから読み込む [name setStringValue:[[NSMutableString alloc] initWithContentsOfFile:[opPanel filename]]]; } }