2

オブジェクトをplistファイルにアーカイブし、後でそれをロードしてtableViewを埋めようとしていました。ファイルは正しくアーカイブされているようですが、ファイルから値を取得しようとするとアクセスが悪くなります。

私は何か間違ったことをしていますか?

これは私がそれを保存する場所です

// Create some phonebook entries and store in array
NSMutableArray *book = [[NSMutableArray alloc] init];
Phonebook *chris = [[Phonebook alloc] init];
chris.name = @"Christian Sandrini";
chris.phone = @"1234567";
chris.mail = @"christian.sandrini@example.com";
[book addObject:chris];
[chris release];

Phonebook *sacha = [[Phonebook alloc] init];
sacha.name = @"Sacha Dubois";
sacha.phone = @"079 777 777";
sacha.mail = @"info@yzx.com";
[book addObject:sacha];
[sacha  release];

Phonebook *steve = [[Phonebook alloc] init];
steve.name = @"Steve Solinger";
steve.phone = @"079 123 456";
steve.mail = @"steve.solinger@wuhu.com";
[book addObject:steve];
[steve release];

[NSKeyedArchiver archiveRootObject:book toFile:@"phonebook.plist"];

そしてここで私はそれをファイルから取り出して配列に戻そうとします

- (void)viewDidLoad {
    // Load Phone Book
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:@"phonebook.plist"];

    self.list = arr;

    [arr release];
    [super viewDidLoad];
}

セルを作ろうとする部分

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *PhoneBookCellIdentifier = @"PhoneBookCellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PhoneBookCellIdentifier];

    if ( cell == nil )
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:PhoneBookCellIdentifier] autorelease];
    }

    NSUInteger row = [indexPath row];
    Phonebook *book = [self.list objectAtIndex:row];
    cell.textLabel.text = book.name;   
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    return cell;
}

ここで不正アクセスエラー

現在の言語:自動; 現在objective-cアサーションに失敗しました:(cls)、関数getName、ファイル/SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm、3990行目。アサーションに失敗しました:(cls)、関数getName、ファイル/ SourceCache /objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm、3990行目。アサーションに失敗しました:(cls)、関数getName、ファイル/SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm、 3990行目。アサーションに失敗しました:(cls)、関数getName、ファイル/SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm、3990行目。

4

1 に答える 1

2

その一部を拡張するだけでunarchiveObjectWithFile、自動解放されたポインタが返されます。あなたはそれをローカルretainにしないので、そうすべきではありませんrelease。そうするので、オブジェクトはその後割り当てが解除され、を呼び出して使用するようになるまでに、オブジェクトはbook.name存在しません。

self.list(ここでリリースしない限りオブジェクトが保持されるように、プロパティが適切に保持されていると想定しています。そうでない場合は、それも修正する必要があります。)

于 2010-05-18T09:41:44.920 に答える