macOS/iOS API解説

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

目次

encodeConditionalObject:

条件付きでオブジェクトをエンコードします
-(void)encodeConditionalObject:(id)object:

解説

objectで指定されたオブジェクトを条件付きでオブジェクトをエンコードします。

このメソッドはencodeWithCoder:メソッド以外のメソッドから呼び出してはいけません。

オブジェクトがnilなら無条件でエンコードします。

ルートオブジェクトがエンコードされない場合はNSInvalidArgumentExceptionを起こします。

返り値

( void )

なし

引数

( id )object

オブジェクト

クラス

NSArchiver

Instance Methods

使用可能

10.0

参照

例文

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

- (IBAction)slider:(id)sender
{

}


- (IBAction)archive:(id)sender
{
    //セーブパネル設定
    NSSavePanel *savePanel = [NSSavePanel savePanel];
    //拡張子codeにする
    [savePanel setRequiredFileType:@"code"];
    //ウインドウのコンテンツビュー丸ごとアーカイブ
    switch ([savePanel runModalForDirectory:NSHomeDirectory() file:@""]) {
        case NSOKButton:
            [[NSArchiver alloc] encodeConditionalObject:[window contentView]];
            break;
    }
}

- (IBAction)unarchive:(id)sender
{
    //拡張子codeのファイルを選ぶ
    NSArray *fileTypes = [NSArray arrayWithObject:@"code"];
    //オープンパネル設定
    NSOpenPanel *openPanel = [NSOpenPanel openPanel];
    //ホームディレクトリで開く
    [openPanel setDirectory:NSHomeDirectory()];
    //
    if ([openPanel runModalForTypes:fileTypes]) {
        id aView = [NSUnarchiver unarchiveObjectWithFile:
            [openPanel filename]];
        [(NSWindow *)window setContentView:aView];
    }

}

@end