开发者

DefaultValue for PSMultiValueSpecifier

开发者 https://www.devze.com 2023-03-01 07:19 出处:网络
I have an iPhone app where I have defined an Settings.bundle with the following settings: <dict>

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>
       开发者_JAVA百科     <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.


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:].

0

精彩评论

暂无评论...
验证码 换一张
取 消