0

I am using this struct to save a token in my app:

struct LocalStore {

    static let userDefaults = NSUserDefaults.standardUserDefaults()

    static func saveToken(token: String) {
        userDefaults.setObject(token, forKey: "tokenKey")
    }

    static func getToken() -> String? {
        return userDefaults.stringForKey("tokenKey")
    }

    static func deleteToken() {
        userDefaults.removeObjectForKey("tokenKey")
    }

}

I know that I wan overwrite existing saved object but can I save multiple objects? Like this:

static func saveToken(token: String) {
        userDefaults.setObject(token, forKey: "tokenKey")
    }

static func saveFirstName(firstName: String) {
        userDefaults.setObject(firstName, forKey: "lastNameVal")
    }

static func saveLastName(lastName: String) {
        userDefaults.setObject(lastName, forKey: "firstNameVal")
    }
7
  • did you mean static func saveFirstName(firstName: String) { userDefaults.setObject(firstName, forKey: "lastNameVal") } static func saveLastName(lastName: String) { userDefaults.setObject(lastName, forKey: "firstNameVal") } I think you have a typo ? Commented Apr 13, 2016 at 16:37
  • What do you want to achieve? Passing data from one end of the app to the other? Commented Apr 13, 2016 at 16:40
  • @EugeneZhenyaGordin Yes sry, I have edited my question now Commented Apr 13, 2016 at 16:53
  • @vikingosegundo I have edited my question. I wonder is its ok to create/save multiple objects/keys Commented Apr 13, 2016 at 16:56
  • 1
    and my question remains: why would you do that (hint: probably you are abusing NSUserDefaults) Commented Apr 13, 2016 at 16:57

2 Answers 2

2

Yes, as long as it has a different key. This is possible, however, from your example, you are saving your token object to multiple keys. There is nothing wrong with that if that is your intention.

User defaults is a form of persistent storage that can save many types of values (not just Strings).

Sign up to request clarification or add additional context in comments.

2 Comments

I have edited my question since I typed the store functions wrong. What I wonder is if its OK to save multiple objects/keys like above?
yes, that's ok to store it like above as long as your key values are distinct, otherwise you'll be overwriting the values for the key
1

The accepted answer is right, but I want to show another way of how to deal with NSUserDefaults in Swift.

By using computed properties instead of methods or functions, it is easy to use NSUserDefaults as the backing variable — and as all set and get operations are performed through this property, no further code is needed to ensure that the property has the correct value.

From production code:

var sorting:DiscoverSortingOrder {
    set {
        NSUserDefaults.standardUserDefaults().setInteger(newValue.rawValue, forKey: "DiscoverSorting")
        refresh() // will trigger reloading the UI
    }
    get {
        if let s = DiscoverSortingOrder(rawValue: NSUserDefaults.standardUserDefaults().integerForKey("DiscoverSorting")){
            return s
        }
        return .New // Default value
    }
}

2 Comments

Thanks for sharing this! To answer your last comment - Everything works fine now, the main reason I am using NSUserDefaults.standardUserDefaults() instead of CoreData is that I only need to save 1 - 3 strings. I am still very new to swift so please correct me if its bad design to not use CoreData in my case
Model doesnt equal Core Data. If any user object would represent the same user, you could also use NSUserDefaults as backing store.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.