Non-static members in Foundation classes


(Marko Havu) #1

Can someone explain what is the issue with non-static members here:

import Foundation

let defaults = UserDefaults.standard  // E642: Cannot access non-static member "standard" on type "NSUserDefaults!"
let userValues = defaults.object(forKey: "UserValues") as? [String: String]
if (userValues == nil || userValues!.count == 0) {
    // No static member "main" on type "Bundle":
    if let path = Bundle.main.path(forResource: "Defaults", ofType: "plist"),
        let userDict = NSDictionary(contentsOfFile: path) as? [String: AnyObject] {
        if (userDict["UserValues"] as? String) != nil {
            defaults.set(["test": "test value"], forKey: "UserValues")
            defaults.synchronize()
        }
    }
}


(marc hoffman) #2

My guess is these are just called differently, because Apple’s rules for mangling Cocoa’s beatiful naming conventions are inconsistent. UserDefaults.standardUserDefaults and Bundle.mainBundle should work.