NSKEYEDUNARCHIVER - Acesso ruim
-
27-09-2019 - |
Pergunta
Eu estava tentando arquivar um objeto em um arquivo de plista e carregá -lo mais tarde para preencher um tabela de tabela. Parece que o arquivo é arquivado corretamente, mas tenho um acesso ruim ao tentar obter o valor do arquivo.
Estou fazendo algo errado?
É aqui que eu salvo
// 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"];
E aqui eu tento tirá -lo do arquivo e salvá -lo de volta em uma matriz
- (void)viewDidLoad {
// Load Phone Book
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:@"phonebook.plist"];
self.list = arr;
[arr release];
[super viewDidLoad];
}
A parte em que tento construir uma célula
- (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;
}
Aqui o erro de acesso ruim
Idioma atual: automático; Atualmente, a afirmação do Objective-C falhou: (CLS), function getName, file /sourcecache/objc4_sim/objc4-427.5/runtime/objc-runtime-new.mm, linha 3990. Aserção falhou: (cls), função getName, arquivo/fonte de fonte 3990. /objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm, line 3990. Assertion failed: (cls), function getName, file /SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm, Linha 3990. Falha na afirmação: (CLS), função getName, arquivo /sourcecache/objc4_sim/objc4-427.5/runtime/objc-runtime-new.mm, linha 3990.
Solução
Apenas para expandir isso uma fração: unarchiveObjectWithFile
retornará um ponteiro autorizado. Você não localmente retain
então você não deveria release
. Porque você faz, o objeto é posteriormente desalocado e quando você o usar, ligando para book.name
, não está lá.
(Estou assumindo o self.list
A propriedade está mantendo adequadamente para que o objeto seja mantido por aí, desde que você não seja lançado aqui. Caso contrário, você precisará consertar isso também.)