macOS/iOS API解説

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

目次

initWithContentsOfFile:ofType:

ファイルからドキュメントオブジェクトを初期化して返します
-(id)initWithContentsOfFile:(NSString *)fileName:
             ofType:(NSString *)docType:

解説

ファイルからドキュメントオブジェクト(NSDocument)を初期化して返します。

返り値

( id )

NSDocumentオブジェクト

引数

( NSString * )fileName

ファイル名

( NSString * )docType

文書タイプ

フレームワーク

ApplicationKit

クラス

NSDocument

Instance Methods

使用可能

10.0

参照

例文

#import "MyObject.h"
#import "MyDocument.h"
@implementation MyObject

- (IBAction)myAction:(id)sender
{
//開けるファイル拡張子の配列
    NSArray      *fileTypes    = [ NSArray arrayWithObjects : @"txt",@"'TEXT'",nil ];
    //OpenPanelを作る
    NSOpenPanel  *opPanel       = [ NSOpenPanel openPanel ];
    //
    MyDocument *doc;
    //OpenPanelの結果のボタン番号
    int		  opRet;
     
        //OpenPanelでファイル選択   
    opRet = [ opPanel runModalForDirectory : NSHomeDirectory() //どこのディレクトリを出すか
                                     file : @"Documents" //どのファイルを選択しておくか
                                    types : fileTypes ];//選べるファイルタイプ

    if ( opRet == NSOKButton ) {  // OPENPanelのボタンがOKなら
        //ファイルから読み込む
    doc = [[MyDocument alloc] initWithContentsOfFile:[opPanel filename] ofType:@"txt"];

    }

}

@end