DefaultValue for PSMultiValueSpecifier

笑着哭i 提交于 2020-01-15 03:41:08

问题


I have an iPhone app where I have defined an Settings.bundle with the following settings:

<dict>
        <key>DefaultValue</key>
        <integer>2</integer>
        <key>Key</key>
        <string>calculationMethod</string>
        <key>Title</key>
        <string>CALCULATION_METHOD</string>
        <key>Titles</key>
        <array>
            <string>Method A</string>
            <string>Method B</string>
            <string>Method C</string>
            <string>Method D</string>
            <string>Method E</string>
            <string>Method F</string>
            <string>Method G</string>
        </array>
        <key>Type</key>
        <string>PSMultiValueSpecifier</string>
        <key>Values</key>
        <array>
            <integer>3</integer>
            <integer>2</integer>
            <integer>5</integer>
            <integer>4</integer>
            <integer>1</integer>
            <integer>6</integer>
            <integer>0</integer>
        </array>
    </dict>

As you can see I want the "Method B" to be selected by default which I define by setting the DefaultValue. However, this only selects the "Method B" in the list, but the actual value returned is by [settings integerForKey:@"calculationMethod"] is 0, which corresponds to "Method G". Am I forgetting something here or is this not the way DefaultValue works at all?

PS. After I change the selection to something else and then back to "Method B" I get the correct value.


回答1:


It's not clear what you mean by [settings integerForKey:@"calculationMethod"]. If settings is an instance of NSUserDefaults, what you are seeing is indeed the expected behavior. The default values in the Settings.bundle only control what is displayed for your app in the Settings app. They have no influence at all on the preferences you load from NSUserDefaults while your app is running.

To get the same default values into the user defaults, you will have to create a second plist that contains the keys (@"calculationMethod") and their default values in a dictionary. When your app launches, open this plist file and pass it to -[NSUserDefaults registerDefaults:].



来源:https://stackoverflow.com/questions/5809869/defaultvalue-for-psmultivaluespecifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!