Search code examples
iphoneobjective-cplistnsbundle

Why redefine pListPath in didSelectRowAtIndexPath?


So I've read a ton of SO-questions about plists and how to save to them, and although I don't know, why no iPhone-Dev-Book I've seen so far covered this (they all used the tableView editing function), I managed to REALLY write to a plist by copying it to the documents folder like this:

pListPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
pListPath = [pListPath stringByAppendingPathComponent:@"piggyBanks.plist"];
NSLog(@"Path: %@", pListPath);

// if the file does not exist yet, create it, and copy the plist data into it, that can be found in the bundle 
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath:pListPath]) {
    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"piggyBanks" ofType:@"plist"];
    [fileManager copyItemAtPath:sourcePath toPath:pListPath error:nil]; 
}

// make the plist content available for usage
pListContent = [[NSMutableDictionary alloc] initWithContentsOfFile:pListPath];
NSLog(@"pListContent: %@", pListContent);

So far so good, but now, if I wanna change some plist value when a user taps on a tableViewCell (it's a custom one, if that's important), although pListPath, pListContent and others are properties, defined in .h and synthesized in .m, I have to redefine pListPath and pListContent inside didSelectRowAtIndexPath, to get the path to be known in that moment.

Could someone please tell me why? I mean, it's nice, that it works, but I'd like to know, why it has to be like that, or if I did a mistake somewhere else..

Thanks!


Solution

  • I googled for class variables in objective C, and found my way through various articles, till i found this blog/blogpost, which explains the self. and _underscore thing really well! I now always declare my Ivars with an underscore, the properties without. Works out quite well. And to sum it up, why to ALWAYS use self.yourPropertiesName, you are calling a method (setter and getter)! And like any other method you are calling, you need to say, who is calling.

    Hope this will help someone else too :)