为什么要在didSelectRowAtIndexPath中重新定义pListPath?
所以我读了很多关于 plist 以及如何保存到它们的问题,虽然我不知道,为什么到目前为止我见过的 iPhone-Dev-Book 没有涵盖这个(它们都使用了 tableView 编辑函数),我设法通过将其复制到文档文件夹来真正写入 plist,如下所示:
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);
到目前为止一切都很好,但是现在,如果我想在用户点击 tableViewCell 时更改某些 plist 值(这是一个自定义值,如果这很重要),尽管 pListPath
、pListContent
和其他属性是在 .h 中定义并在 .m 中合成的,但我必须重新定义 pListPath
并didSelectRowAtIndexPath
内的 pListContent
,获取当时已知的路径。
有人可以告诉我为什么吗?我的意思是,这很好,它有效,但我想知道,为什么它必须是这样,或者我是否在其他地方犯了错误......
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
plistPath
是类的字符串属性,则需要按如下方式分配它:然后,使用
self.pListPath
而不是pListPath
。您必须重新设置它,因为目前,
pListPath
被分配给一个自动释放的字符串,当您再次需要它时,该字符串将被删除。设置属性(假设属性被保留或复制)将为您保留字符串。If
plistPath
is a string property of your class, you need to assign it as such:Then afterwards, use
self.pListPath
instead ofpListPath
.You are having to re-set it because at the moment,
pListPath
is being assigned to an autoreleased string which will have been removed by the time you need it again. Setting the property (assuming the property is retained or copied) will retain the string for you.我在目标 C 中搜索类变量,并通过各种文章找到了方法,直到我找到 这个博客/博客文章,解释了自我。和 _underscore 事情真的很好!
我现在总是用下划线声明我的 Ivar,而没有下划线的属性。效果很好。总而言之,为什么总是使用 self.yourPropertiesName,您正在调用一个方法(setter 和 getter)!与您调用的任何其他方法一样,您需要说明谁在调用。
希望这对其他人也有帮助:)
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 :)