2

NSString タイトル、NSString テキスト、および NSDate creationDate を保持するカスタム 'Entry' オブジェクトを使用する、最初の iPhone 'Diary' アプリを開発しています。Entry オブジェクトの NSMutableArray をアーカイブし、後でビューが読み込まれたときにそれらを取得しようとすると、アプリがクラッシュします。NSKeyedArchivers を使用するサンプル コードと例をたくさん調べましたが、なぜそれが起こるのかまだわかりませんでした。エントリを保持する配列の初期化に問題があると推測していますが、わかりません...

これがコードです。私がずっと監視してきた何かを見つけることができるかもしれません...」

//--------- Entry.m---------------

- (id) initWithCoder:(NSCoder *)aDecoder{

if ((self = [super init])) {
    self.title = [[aDecoder decodeObjectForKey:@"title"] retain];
    self.text = [[aDecoder decodeObjectForKey:@"text"] retain];
    self.created = [[aDecoder decodeObjectForKey:@"created"] retain];
}
return self;

}

- (void) encodeWithCoder:(NSCoder *)aCoder{

[aCoder encodeObject:self.title forKey:@"title"];
[aCoder encodeObject:self.text forKey:@"text"];
[aCoder encodeObject:self.created forKey:@"created"];

}

//-------------- Diary View Controller.m

- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}

- (void) writeDataToArchive {
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
                             initForWritingWithMutableData:data];

[archiver encodeObject:self.entriesArray forKey:@"entriesArray"];
[archiver finishEncoding];
BOOL result = [data writeToFile:[self dataFilePath]  atomically:YES];
[archiver release];
[data release];    
}

- (void)addItem:sender {
int count = [entriesArray count] +1;    
NSString *newEntryTitle = [NSString stringWithFormat:@"Entry %d", count];    
Entry *anEntry = [[Entry alloc] initWithTitle:newEntryTitle text:@"-" 
         created:[NSDate date]];
[entriesArray addObject:anEntry];
[self.tableView reloadData];

[anEntry release];
[self writeDataToArchive];
}

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {     
     NSData *data = [[NSMutableData alloc]
                     initWithContentsOfFile:[self dataFilePath]];

     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
          initForReadingWithData:data];
     NSMutableArray *array = [unarchiver decodeObjectForKey:@"entriesArray"];     
     entriesArray = [array mutableCopy];
     [array release];
     [unarchiver finishDecoding];
     [unarchiver release];
     [data release];
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
    (NSIndexPath *)indexPath
{
    // ... some other stuff
     NSUInteger row = indexPath.row;
     Entry *entry = [entriesArray objectAtIndex:row];

     cell.textLabel.text = entry.title;
     return cell;
}

どうもありがとう。

4

2 に答える 2

3

NSKeyedUnarchivers で配列を読み戻すと、常に変更不可能なコピーが返されます。*array を NSArray として宣言するか、配列をすべて削除する必要があります。

entriesArray = [[unarchiver decodeObjectForKey:@"entriesArray"] mutableCopy]; 

そして @JeremyP は別の問題を指摘しています。*array を割り当てたり保持したりしていないため、解放しないでください。

于 2011-05-09T17:02:05.217 に答える
2

所有していないため、 リリースarrayしないでください。viewDidLoad

コードには他にもいくつかのメモリ管理の問題があるため、Cocoa メモリ管理ルールを確認してください。特に、

self.title = [[aDecoder decodeObjectForKey:@"title"] retain];
self.text = [[aDecoder decodeObjectForKey:@"text"] retain];
self.created = [[aDecoder decodeObjectForKey:@"created"] retain];

initWithCoder:メソッドでは、プロパティが保持またはコピーされていると仮定してすべてのリークが発生します。

于 2011-05-09T10:31:21.777 に答える