2011-07-11 39 views
0

這裏是我的代碼:編輯外部屬性格式列表(的.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:]: this class is not key value coding-compliant for the key Bundle identifier.

我該如何解決這個問題?

回答

1

關鍵不是'捆綁標識符' - 這是密鑰的友好表示。

實際的關鍵是 'CFBundleIdentifier',並有一個CFStringRef常數,它可以強制轉換爲NSString

(NSString *)kCFBundleIdentifierKey 

編輯:與您的代碼更多的問題:

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

infoPlist是一個字符串,其中包含Info.plist文件的文件路徑n應用程序包。這不是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]; 
+0

我仍然得到'的setValue:forUndefinedKey:]:此類不是密鑰值編碼兼容的關鍵CFBundleIdentifier.' ... – Sam

+0

哦,謝謝!現在正在工作。 :) – Sam