Pergunta

I've been following this guide on saving data to a plist, using NSCoding. I've followed the guide step by step as far as i can tell, as well as looked at the sample code, but when it comes to actually saving the data, it doesn't seem to save it, nor does it seem to call the save function. I'm calling the save function via a button:

- (IBAction)saveAll:(id)sender {
    NSLog(@"start saveall");
    _conosirDoc.data.wineType = _wineType.text;
    _conosirDoc.data.wineTitle = _wineTitle.text;
    _conosirDoc.data.wineYear = _wineYear.text;
    _conosirDoc.data.wineVolume = _wineVolume.text;
    _conosirDoc.data.wineRating = _wineRating.text;
    _conosirDoc.data.wineCountry = _wineCountry.text;
    _conosirDoc.data.wineRegion = _wineRegion.text;
    _conosirDoc.data.wineGrapes = _wineGrapes.text;
    [_conosirDoc saveData];
    NSLog(@"%@", _conosirDoc.data.wineType);
    NSLog(@"end saveall");
}

so when the button is clicked the log reads :

start saveall
(null)
end saveall

so from there I set up the following NSLog's in the saveData function:

- (void)saveData {
    NSLog(@"being called?");
    if (_data == nil) return;
    NSLog(@"START");
    [self createDataPath];
    NSLog(@"END");
    NSString *dataPath = [_docPath stringByAppendingPathComponent:kDataFile];
    NSLog(@"%@", dataPath);
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:_data forKey:kDataKey];
    [archiver finishEncoding];
    [data writeToFile:dataPath atomically:YES];

}

and from this, none of these NSLogs appear in the output, and i can't work out why it's not being called properly. Let me know if you need anymore information.

Thanks.

Foi útil?

Solução

The log lines that you see (and especially the ones that you do not see) indicate that _conosirDoc is nil - it has not been initialized: the only reason the saveData would not log the unconditional "being called?" is that saveData is invoked on a nil. In all other cases there would be at least that additional log line.

You need to add code that assigns an object to your _conosirDoc variable to make it work:

_conosirDoc = [[ConosirDocType alloc] init];

Replace ConosirDocType above with the actual type of _conosirDoc.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top