Settings bundle values returning nil

后端 未结 4 1607
青春惊慌失措
青春惊慌失措 2021-01-04 22:25

I added a Settings bundle to my app and in Xcode it appears in the root of my project tree view.

The Root.plist file looks like this:

&l         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-01-04 23:04

    Apparently the cause is that if my settings in the plist have defaults defined and the user has not explicitly set a value, then the value displayed in the Settings app will be the defaults from the plist file, however the NSUserDefaults API will still return nil.

    Unfortunately this means that if the default value is meaningful (such as a default web-service address URI: "http://www.example.com") it must exist twice in my project: as a default in the plist and in my program code:

    Root.plist:

      
          Key          mySettingKey
          Title        Some address
          Type         PSTextFieldSpecifier
          DefaultValue http://www.example.com
          IsSecure     
          KeyboardType Alphabet
      
    

    Program.swift:

    let ud = NSUserDefaults.standardUserDefaults()
    ud.synchronize()
    
    var mySettingValue = ud.stringForKey("mySettingKey")
    if mySettingValue == nil {
        mySettingValue = "http://www.example.com"
    }
    

    That's surprising.

提交回复
热议问题