如何在NSUserDefaults中设置NSArray?
问题描述:
我想知道为什么有-arrayForKey:方法,但没有-setArrayForKey:方法。我该如何设置该数组?
I'm wondering why there is a -arrayForKey: method, but no -setArrayForKey: method. How could I set that array?
答
只需使用 setObject:forKey:
。我猜有一个特殊的 arrayForKey:
和 dictionaryForKey:
等的原因是,您可以获得更好的类型安全性,而不必不必投。如果使用这些方法,并且默认设置中的元素与类型不匹配,则返回 nil
。
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.