views:

461

answers:

3

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

+2  A: 

- (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.

Nick Veys
+2  A: 

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.

Rüdiger Hanke
Note that the -arrayForKey: etc. methods do give you one extra feature. If the object in the defaults of an unexpected type (e.g. a string, perhaps entered directly by the user), it will be ignored, and nil returned.
Mike Abdullah
That's what the last sentence was supposed to mean :).
Rüdiger Hanke
+1  A: 

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

thomax