A
A
Anton B2015-06-08 15:14:11
iOS
Anton B, 2015-06-08 15:14:11

Should I use a generic data type in NSUserDefaults?

Hello.
My question is rather from the category of understanding the principles of programming than about a specific implementation.
I need a generic wrapper to save and retrieve user preferences. Sometimes it will be necessary to store a string, sometimes an array. Is it correct to use AnyObject or do you need to create your own set() function for each data type?

class UserData {
    
    func set(key: String, value: AnyObject) {
    
        NSUserDefaults.standardUserDefaults().setObject(value, forKey: key)
        NSUserDefaults.standardUserDefaults().synchronize()    
    }

    func get(key: String) -> AnyObject? {
    
        return NSUserDefaults.standardUserDefaults().objectForKey(key)
    }
}

Thanks a lot.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question