编辑外部属性列表 (.plist)
这是我的代码:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
- (IBAction)unlockIt:(id)sender {
if ([[appField stringValue] length] < 1) {
[status setTextColor:[NSColor redColor]];
[status setStringValue:@"error with name"];
}
else {
[status setStringValue:@""];
[status setTextColor:[NSColor greenColor]];
NSString *path = [NSString stringWithFormat:@"/Applications/%@.app/Contents", [appField stringValue]];
NSBundle *bundle = [NSBundle bundleWithPath:path];
NSString *infoPlist = [bundle pathForResource:@"Info" ofType:@"plist"];
NSString *randomIdentifier = [NSString stringWithFormat:@"com.derp.%i", arc4random()];
[infoPlist setValue:randomIdentifier forKey:@"Bundle identifier"];
[status setStringValue:@"attempt successful"];
}
}
我收到此错误:
[ setValue:forUndefinedKey:]:此类对于键 Bundle 标识符不符合键值编码。
我该如何解决这个问题?
Here is my code:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
- (IBAction)unlockIt:(id)sender {
if ([[appField stringValue] length] < 1) {
[status setTextColor:[NSColor redColor]];
[status setStringValue:@"error with name"];
}
else {
[status setStringValue:@""];
[status setTextColor:[NSColor greenColor]];
NSString *path = [NSString stringWithFormat:@"/Applications/%@.app/Contents", [appField stringValue]];
NSBundle *bundle = [NSBundle bundleWithPath:path];
NSString *infoPlist = [bundle pathForResource:@"Info" ofType:@"plist"];
NSString *randomIdentifier = [NSString stringWithFormat:@"com.derp.%i", arc4random()];
[infoPlist setValue:randomIdentifier forKey:@"Bundle identifier"];
[status setStringValue:@"attempt successful"];
}
}
I get this error:
[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Bundle identifier.
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
密钥不是“捆绑包标识符”——这是密钥的友好表示。
实际的键是“CFBundleIdentifier”,并且有一个
CFStringRef
常量,您可以将其转换为NSString
:编辑: 更多问题与您的code:
infoPlist
是一个字符串,其中包含应用程序包内 Info.plist 文件的文件路径。它不是 plist 本身。您需要将 Info.plist 读入字典,更改其内容,然后再次写入。例如:The key isn’t ‘Bundle identifier’ — that’s the friendly representation of the key.
The actual key is ‘CFBundleIdentifier’, and there’s a
CFStringRef
constant for it that you can cast to anNSString
:Edit: More issues with your code:
infoPlist
is a string containing that file path of the Info.plist file inside an application bundle. It is not the plist itself. You need to read Info.plist into a dictionary, change its contents, and then write it again. For example: