本当にオブジェクトを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];
}
これは、必要に応じてファイルの名前などを返すように簡単に変更できます。