3

Can you use:

[[NSUserDefaults standardUserDefaults] setValue:objectID forKey:"objectID"]

to set an object in the NSUserDefaults and then retrieve that object using:

objectType *tempObject = [[NSUserDefaults standardUserDefaults] objectForKey:@"objectID"]

?

So to clarify, What I'm asking is that if I use setObject, can I use valueForKey ? Or vice versa, can I use setValue and then retrieve it using objectForKey ?

And why can/can't you do that?

If I read correctly, setObject / objectForKey deals with NSDictionary and setValue / valueForKey deals with KVC, correct?

I'm just trying to get to understand the difference between the two a little better.

Thanks. Cheers.

erad
  • 1,766
  • 2
  • 17
  • 26
  • possible duplicate of [Where's the difference between setObject:forKey: and setValue:forKey: in NSMutableDictionary?](http://stackoverflow.com/questions/1249634/wheres-the-difference-between-setobjectforkey-and-setvalueforkey-in-nsmutab) – gran33 Oct 05 '14 at 07:22
  • @gran33. I did see that question, and my question goes a little further than that one. And frankly that question you cited could actually be considered a duplicate of another question. – erad Oct 05 '14 at 07:29

1 Answers1

8

From the documentation

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

The value parameter can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. For NSArray and NSDictionary objects, their contents must be property list objects. See

That works with setObject / objectForKey where you are adding objects inside the NSUserDefaults

Your example will work if objectID is only one type of the six above. Why? because NSUserDefaults takes that object and save it to the file system in a plist file and so it needs to know how to deal with that object to save it. If you added a custom object then the NSUserDefaults will not be able to save that on the file system

.

While setValue / valueForKey works with the properties of the class NSUserDefaults itself not with the objects inside it that you want to save


Edit

So to clarify, What I'm asking is that if I use setObject, can I use valueForKey ? Or vice versa, can I use setValue and then retrieve it using objectForKey ?

Short Answer: Yes You can

Long Answer: NSUserDefaults override the valueForKey and setValueForKey to behave like objectForKey and setObjectForKey

However if the key is the same as a property in the NSUserDefaults then conflicts may happenes ... but in the case of NSUserDefaults where it has no properties then you can use it. But in my opinion you should use the standard methods for readably specially if that is not documented

Hani Ibrahim
  • 1,448
  • 11
  • 21
  • I did read that documentation before I posted this question. What I'm asking is that if I use `setObject`, can I use `valueForKey` ? Or vice versa, can I use `setValue` and then retrieve it using `objectForKey` ? – erad Oct 05 '14 at 07:32
  • @erad Please update the question again with the above statement. – raurora Oct 05 '14 at 07:33