0

モデル オブジェクト クラス Box があるとします。私の Box クラスでは、画像参照 (png)、オーディオ (mp3) などを追加します...それらを NSData として保存するよりも、ファイルへのパスを参照する方が良いようです...メモリを節約します。

この Box クラスをアーカイブしたいと思います。デスクトップでは、ドキュメント パッケージ (NSFilewrapper) を使用します。しかし、このクラスは Iphone OS の一部ではありません。

このクラスをアーカイブし、すべてのファイルを「ドキュメント パッケージ」として含めることに関する提案はありますか? これは、アプリケーションがファイルとして表示される方法に似ていますが、実際にはフォルダーです... ありがとうございます!

4

1 に答える 1

0

本当にオブジェクトをBoxに保存する必要がある場合は、それらをNSDictionaryにロードして、次のように書き出します。

注:これは、開始点としてのみ使用することを目的とした、テストされていない/非実稼働のコードです。

- (BOOL)saveBox:(Box*)aBox;
{
    NSMutableDictionary *boxDict = [NSMutableDictionary dictionaryWithCapacity:10];

    // Add contents of box to dictionary (exercise left up to original poster)

    // boxDict is now populated

    // write dictionary to apps document directory.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    if (!documentsDirectory) {
        NSLog(@"Documents directory not found!");
        return NO;
    }

    // Assumes Box class has name property
    NSString *outputFile = [documentsDirectory stringByAppendingPathComponent:[aBox name]];

    return [boxDict writeToFile:outputFile atomically:YES];

}

これは、必要に応じてファイルの名前などを返すように簡単に変更できます。

于 2010-03-03T20:33:47.327 に答える