Jules
Jules

Reputation: 7766

NSUserDefaults setObject and IntegerForKey, use on the same key, if not how can I workaround?

I need to iterate through all the keys in my NSUserDefaults and transfer them to another format. Later I'll want to transfer them back, programatically.

I have a lot of keys and I have no way in my naming convention or by other means to determine if I used setObject or setInteger etc.

If I use setObject will this enable me to use integerForKey on that key ?

If I can't want can I use instead?

Upvotes: 0

Views: 589

Answers (2)

MadhavanRP
MadhavanRP

Reputation: 2830

No, you should not be able to get the correct objects if you use integerForKey:. However you could get the correct objects if you use objectForKey:. This is because if you had used setInteger:forKey:, it automatically calls setObject:forKey:. So, there will be an object for the key. So what you have to do is iterate through the keys, get the objects using objectForKey: and convert it to the data type that you want.

Upvotes: 1

RDM
RDM

Reputation: 5066

I'm pretty sure using setObject will allow you to use integerForKey later.

Another way to go could be by adding a plist to your project and store everything in there. It's basically the same as NSUserDefaults is doing, only you'll be doing it all by hand. You can just read the contents of a plist to either an NSArray or an NSDictionary (whichever you chose to construct it with in the first place) and then access the members you need from that object.

Hope this helps.

Upvotes: 0

Related Questions