phimage / Prephirences

Prephirences is a Swift library that provides useful protocols and convenience methods to manage application preferences, configurations and app-state. UserDefaults
MIT License
573 stars 35 forks source link

NSUserDefaults does not have a member named 'subscript' #1

Closed sebastiaanluca closed 9 years ago

sebastiaanluca commented 9 years ago

Don't know if I'm doing something wrong here, but just tried the example and it throws an error.

let userDefaults = NSUserDefaults.standardUserDefaults()

if let myValue = userDefaults["mykey"] as? Bool {..}

NSUserDefaults does not have a member named 'subscript'

If I use objectForKey, it works.

let userDefaults = NSUserDefaults.standardUserDefaults()

if let myValue = userDefaults.objectForKey("mykey") as? Bool {..}

Same situation when trying to modify it. userDefaults["mykey"] = "myvalue" does not work.

phimage commented 9 years ago

objectForKey is standard method of NSUserDefaults class, work without using Prephirences framework Did you use cocoapod? and import the framework in your file?

import Prephirences

For information subscript for NSUserDefaults is defined in NSUserDefaults+Prephirences.swift

sebastiaanluca commented 9 years ago

The import was the solution! Thought it was a Swift extension of NSUserDefaults, so I could use it without importing anything. Now I can successfully use return self.userDefaults[key] and return self.userDefaults[key]?[preference]. Thanks!

PS: maybe add it to the readme? For us noobs ;)