Frage

After reading from various answers I have come to know that NSUserDefaults can save multiple datatypes for one key. But what I cannot find is if

[NSUserDefaults standardUserDefaults] removeObjectForKey:"someKey"];

removes all objects of all data types associated with that key?

War es hilfreich?

Lösung

You cannot store different kind of objects for one key.

If you set an object for a key it will erase the old one.

But, if your are searching for a way to store multiple data for one key, you can store a NSDictionary.

Ex :

MyObject *obj = [[MyObject alloc] init];
NSString *otherType = @"mystring";

NSDictionary *multipleData = @{ @"key1" : obj , @"key2" : otherType}

[[NSUserDefaults standardUserDefaults] setObject: multipleData forKey:@"multipleData"];
[[NSUserDefaults standardUserDefaults] synchronize];

And if you want to remove it :

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"multipleData"];
[[NSUserDefaults standardUserDefaults] synchronize];

Andere Tipps

Yes it does.

Your data may be anything an array or dictionary or simple int. This command will remove that data.

As iPatel suggested. You need to call:

[[NSUserDefaults standardUserDefaults] synchronize];

After adding or deleting any data. Hope this helps.. :)

You cannot store multiple objects under one key. NSUserDefaults acts just like a NSDictionary. When you set an object for a specific key you overwrite the old object. So removeObjectForKey: just removes one object/value; the one you had stored under that key.

Do you call

[[NSUserDefaults standardUserDefaults] synchronize];

after delete all the data of the key and also might be you can not store multiple data on single key, it's return new one that inserted to last. ?

Read official documentation of removeObjectForKey of NSUserDefaults.

    NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSString *yourDomain = [[NSBundle mainBundle] bundleIdentifier];
[userDefault removePersistentDomainForName:yourDomain];

Here. if u want to reset.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top