这是我的代码:
- (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标识符。
我该如何解决这个问题?
答案 0 :(得分:1)
密钥不是'Bundle identifier' - 这是密钥的友好表示。
实际的密钥是'CFBundleIdentifier',并且它有一个CFStringRef
常量可以转换为NSString
:
(NSString *)kCFBundleIdentifierKey
修改:您的代码存在更多问题:
NSString *infoPlist = [bundle pathForResource:@"Info" ofType:@"plist"];
[infoPlist setValue:randomIdentifier forKey:@"Bundle identifier"];
infoPlist
是一个字符串,包含应用程序包内Info.plist文件的文件路径。这不是plist本身。您需要将Info.plist读入字典,更改其内容,然后再次编写。例如:
NSString *infoPlistPath = [bundle pathForResource:@"Info" ofType:@"plist"];
NSMutableDictionary *infoPlist = [NSDictionary dictionaryWithContentsOfFile:infoPlistPath];
[infoPlist setObject:randomIdentifier forKey:(NSString *)kCFBundleIdentifierKey];
[infoPlist writeToFile:infoPlistPath atomically:YES];