简体   繁体   English

如何在NSUserDefaults中设置NSArray?

[英]How to set an NSArray in NSUserDefaults?

I'm wondering why there is a -arrayForKey: method, but no -setArrayForKey: method. 我想知道为什么有一个-arrayForKey:方法,但没有-setArrayForKey:方法。 How could I set that array? 我怎么能设置那个数组?

Just use setObject:forKey: . 只需使用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. 我想有一个特殊的arrayForKey:dictionaryForKey:等等的原因是你获得了更好的类型安全性而且不必进行arrayForKey:转换。 If you use these methods and the element in the defaults does not match the type, then nil is returned. 如果使用这些方法且默认值中的元素与类型不匹配,则返回nil

arrayForKey and dictionaryForKey are getters. arrayForKeydictionaryForKey是getters。 Use setObject to set an array. 使用setObject设置数组。

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

This allows one to set any typical plist object, including an NSArray. 这允许设置任何典型的plist对象,包括NSArray。

arrayForKey appears to just be a convenience method. arrayForKey似乎只是一种方便的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM