问题
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