User1234
User1234

Reputation: 2412

How to get key from object in NSUserDefaults?

I'm using this to get object from Key

 NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
 NSString *object=[defaults objectForKey:someKey];

How I can get opposite effect? Key from Object? Something like

NSString *key=[defaults keyForObject:object];

Thanks

Upvotes: 0

Views: 987

Answers (1)

jbat100
jbat100

Reputation: 16827

You can get the NSUserDefaults dictionary (with the dictionaryRepresentation method). You can then use the dictionary method

- (NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key, id obj, BOOL *stop))predicate

or

- (NSArray *)allKeysForObject:(id)anObject

to get the key for your object.

Upvotes: 5

Related Questions