0

我想通过我的应用程序更改 PSMultiValueSpecifier 标题数组和值数组。这是我的 Root.plist 文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreferenceSpecifiers</key>
<array>
    <dict>
        <key>Type</key>
        <string>PSGroupSpecifier</string>
        <key>Title</key>
        <string>Login Information</string>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSTextFieldSpecifier</string>
        <key>Title</key>
        <string>Restaurant Code</string>
        <key>Key</key>
        <string>Restaurant_Code_key</string>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSTextFieldSpecifier</string>
        <key>Title</key>
        <string>Password            </string>
        <key>Key</key>
        <string>Password_key</string>
        <key>IsSecure</key>
        <true/>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSToggleSwitchSpecifier</string>
        <key>Title</key>
        <string>Remember Login Information</string>
        <key>Key</key>
        <string>Remember_Login_Information_key</string>
        <key>DefaultValue</key>
        <false/>
        <key>TrueValue</key>
        <true/>
        <key>FalseValue</key>
        <false/>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSGroupSpecifier</string>
        <key>Title</key>
        <string>Menu Content</string>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSMultiValueSpecifier</string>
        <key>Title</key>
        <string>Default Language</string>
        <key>Key</key>
        <string>Default_Language_key</string>
        <key>DefaultValue</key>
        <integer>0</integer>
        <key>Titles</key>
        <array>
            <string>English</string>
            <string>Spanish</string>
        </array>
        <key>Values</key>
        <array>
            <integer>0</integer>
            <integer>1</integer>
        </array>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSToggleSwitchSpecifier</string>
        <key>Title</key>
        <string>Update Data on Launch</string>
        <key>Key</key>
        <string>Update_Data_key</string>
        <key>DefaultValue</key>
        <false/>
        <key>TrueValue</key>
        <true/>
        <key>FalseValue</key>
        <false/>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSGroupSpecifier</string>
        <key>Title</key>
        <string></string>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSToggleSwitchSpecifier</string>
        <key>Title</key>
        <string>Reset on Next Launch</string>
        <key>Key</key>
        <string>Reset_key</string>
        <key>DefaultValue</key>
        <false/>
        <key>TrueValue</key>
        <true/>
        <key>FalseValue</key>
        <false/>
    </dict>

</array>
<key>StringsTable</key>
<string></string>
</dict>
</plist>


I tried to change like this Through my application.
<dict>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>Title</key>
<string>Default Language</string>
<key>Key</key>
<string>Default_Language_key</string>
<key>DefaultValue</key>
<integer>0</integer>
<key>Titles</key>
<array>
<string>English</string>
</array>
<key>Values</key>
<array>
<integer>0</integer>
</array>
</dict>

但这不会改变标题和值。请有人帮助我。下面给出了我使用的代码。

NSURL * settingsURL =  [[NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"Settings" withExtension:@"bundle"]]
                        URLForResource:@"Root" withExtension:@"plist"];
NSMutableDictionary * settingsDict = [NSMutableDictionary    dictionaryWithContentsOfURL:settingsURL];
NSMutableArray *settingsArr = [settingsDict objectForKey:@"PreferenceSpecifiers"];

NSMutableDictionary *loTempDict = [[NSMutableDictionary alloc] init];
loTempDict = [settingsArr objectAtIndex:5];

NSArray *title_Array = [loTempDict objectForKey:@"Titles"];
NSArray *value_Array = [loTempDict objectForKey:@"Values"];

NSArray *lanArray_title = [NSArray arrayWithObject:@"French"];
NSArray *lanArray_value = [NSArray arrayWithObject:@"0"];

[loTempDict setObject:lanArray_title forKey:@"Titles"];
[loTempDict setObject:lanArray_value forKey:@"Values"];
[settingsArr replaceObjectAtIndex:5 withObject:loTempDict];


NSLog(@"final settingsArr ===%@ ",settingsArr);

NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];

[settingsArr writeToFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"] atomically: TRUE];

谢谢

4

2 回答 2

0

你不能这样做。出于安全原因,无法写入应用程序包。您能想到的最佳替代方案是将要修改的文件从应用程序包目录复制到库或文档目录 - 这些是可写的,并且可以更新其中的文件。

于 2012-09-22T05:22:59.593 回答
-1

这将解决您的问题

NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];

[settingsDict setValue:settingsArr forKey:@"PreferenceSpecifiers"];

[settingsDict writeToFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"] atomically: TRUE];
于 2012-09-11T07:32:38.453 回答