将密钥添加到 NSDictionary (iPhone SDK)
非常快速地尝试将密钥添加到 .plist。我差不多明白了,第四行的正确版本是什么?
NSString *path = [[NSBundle mainBundle] pathForResource:@"Favourites" ofType:@"plist"];
NSDictionary *rootDict = [[NSDictionary alloc] initWithContentsOfFile:path];
[rootDict addKey:@"Test"]; //guessed code
[rootDict writeToFile:path atomically: YES];
A really quickly trying to add a key to a .plist. I almost have it, what is the correct version of the fourth line?
NSString *path = [[NSBundle mainBundle] pathForResource:@"Favourites" ofType:@"plist"];
NSDictionary *rootDict = [[NSDictionary alloc] initWithContentsOfFile:path];
[rootDict addKey:@"Test"]; //guessed code
[rootDict writeToFile:path atomically: YES];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法向 NSDictionary 添加元素,您需要使用 NSMutableDictionary,然后使用
setObject:forKey:
方法。请参阅 NSMutableDictionary 类参考
You can't add elements to a NSDictionary, you need to use a NSMutableDictionary then use the
setObject:forKey:
method.See NSMutableDictionary class reference
。
您无法更改 NSDictionary。您无法写入主包。
工作代码:
not really.
You can't change a NSDictionary. You can't write to the mainbundle.
working code: