1

我手动向我的 AppName-Info.plist 添加了一个名为“App”的键,我可以通过调用此代码从中获取值

NSBundle *mainBundle;
mainBundle = [NSBundle mainBundle];

NSString *value = [mainBundle objectForInfoDictionaryKey:@"App"];

NSLog(@"App: %@",value);

但我不能做的是用任何代码改变值..有可能吗?如果是的话怎么做?

提前感谢您的帮助:)

4

3 回答 3

4

你应该考虑NSUserDefaults或者如果你想修改一个捆绑的 .plist 试试这个。

    NSString* plistFilePath = nil;
NSFileManager* manager = [NSFileManager defaultManager];
if ((plistFilePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"mySpecial/PathTo.plist"])) 
{
    if ([manager isWritableFileAtPath:plistFilePath]) 
    {
        NSMutableDictionary* infoDictioio = [NSMutableDictionary dictionaryWithContentsOfFile:plistFilePath];
        [infoDictio setObject:@"foo object" forKey:@"fookey"];
        [infoDictio writeToFile:plistFilePath atomically:NO];
        [manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil];
    }
}
于 2013-04-10T11:53:51.610 回答
2

您不应在运行时修改您的应用程序 Info.plist 文件(或应用程序包中的任何内容)。这是不好的做法,还会破坏您的捆绑代码签名(这将导致无法再启动应用程序!)。

如果你想存储设置,你应该看看NSUserDefaults

于 2013-04-10T11:50:17.817 回答
1

这是不可能的。

默认情况下,AppName-Info.plist 不会在构建的 Copy Bundle Resources 阶段复制到包中。

因此,如果您想拥有一个可以写入选项的 plist,则可以在运行时在临时文件位置创建它并在那里读/写它。

是研究如何做到这一点的好地方。

于 2013-04-10T11:52:39.300 回答