macOS/iOS API解説

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

目次

initWithContentsOfFile:

INDEX>Foundation>NSString>

ファイルの内容からNSStringを初期化して返します
-(id)initWithContentsOfFile:(NSString *)path

解説

■10.4以降非推奨。
(initWithContentsOfFile:encoding:error: または initWithContentsOfFile:usedEncoding:error: を使います。)

ファイルの内容からNSStringを初期化して返します。
ファイルがU+FEFFまたはU+FFFEから始まるならばユニコード文字と解釈します。
でなければC文字列と解釈します。
ファイルを開くことが出来なければnilを返します。

返り値

( id )

文字列(NSStringまたはそのサブクラス)

引数

( NSString * )path

読み込むファイルのパス

クラス

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