How to set an NSArray in NSUserDefaults?

好久不见. 提交于 2019-12-22 11:02:32

问题


I'm wondering why there is a -arrayForKey: method, but no -setArrayForKey: method. How could I set that array?


回答1:


Just use setObject:forKey:. I guess the reason that there's a special arrayForKey: and dictionaryForKey: etc. is that you get better type safety and don't have to cast. If you use these methods and the element in the defaults does not match the type, then nil is returned.




回答2:


arrayForKey and dictionaryForKey are getters. Use setObject to set an array.




回答3:


- (void)setObject:(id)value forKey:(NSString *)defaultName

This allows one to set any typical plist object, including an NSArray.

arrayForKey appears to just be a convenience method.



来源:https://stackoverflow.com/questions/1342334/how-to-set-an-nsarray-in-nsuserdefaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!