目标c -编辑外部属性列表(.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 identifier ' -这是键的友好表示。

实际的键是' CFBundleIdentifier ',并且有一个CFStringRef常量,您可以将其转换为NSString:

(NSString *)kCFBundleIdentifierKey

编辑:代码的更多问题:

NSString *infoPlist = [bundle pathForResource:@"Info" ofType:@"plist"];
[infoPlist setValue:randomIdentifier forKey:@"Bundle identifier"];

infoPlist是包含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];

最新更新