编辑外部属性列表 (.plist)

发布于 2024-11-19 15:53:33 字数 1081 浏览 3 评论 0原文

这是我的代码:

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娜些时光,永不杰束 2024-11-26 15:53:34

密钥不是“捆绑包标识符”——这是密钥的友好表示。

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

(NSString *)kCFBundleIdentifierKey

编辑: 更多问题与您的code:

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];

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 an NSString:

(NSString *)kCFBundleIdentifierKey

Edit: More issues with your code:

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

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:

NSString *infoPlistPath = [bundle pathForResource:@"Info" ofType:@"plist"];
NSMutableDictionary *infoPlist = [NSDictionary dictionaryWithContentsOfFile:infoPlistPath];
[infoPlist setObject:randomIdentifier forKey:(NSString *)kCFBundleIdentifierKey];
[infoPlist writeToFile:infoPlistPath atomically:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文