initWithContentsOfURL:ofType:
URLからドキュメントオブジェクトを初期化して返します
-(id)initWithContentsOfURL:(NSURL *)anURL: ofType:(NSString *)docType:
解説
URLからドキュメントオブジェクト(NSDocument)を初期化して返します。
返り値
( id )
NSDocumentオブジェクト
引数
( NSURL * )anURL
URL
( 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; NSURL *theUrl; //OpenPanelの結果のボタン番号 int opRet; //OpenPanelでファイル選択 opRet = [ opPanel runModalForDirectory : NSHomeDirectory() //どこのディレクトリを出すか file : @"Documents" //どのどのファイルを選択しておくか types : fileTypes ];//選べるファイルタイプ if ( opRet == NSOKButton ) { // OPENPanelのボタンがOKなら //ファイルから読み込む theUrl = [NSURL fileURLWithPath:[ opPanel filename ]]; doc = [[MyDocument alloc] initWithContentsOfURL:theUrl ofType:@"txt"]; } } @end