gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:00:48 24 4
gpt4 key购买 nike

这是我的代码:

- (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:]: this class is not key value coding-compliant for the key Bundle identifier.

我该如何解决这个问题?

最佳答案

key 不是“ bundle 标识符”——这是 key 的友好表示。

实际的键是“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];

关于objective-c - 编辑外部属性列表 (.plist),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6655987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com