Answer the question
In order to leave comments, you need to log in
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)
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question