2

I am trying to implemen "Add to Favorites" functionality using NSMutableDictionary as I have to add multiple key-values. The following addToFavourites method always display Count=0 even after adding object to dictionary. I read this and getting nsdefaults into mutabledictionary(instead of mutable array) and setting resulted dictionary back to nsdefaults but not sure what's the problem. Any help would be really appreciated. Thank you.

Edited: From this I came to know that I have to move data around mutable and immutable dictionary and then it worked fine! but still not able to synchronize modified NSMutableDictionary to NSUserDefaults.

-(BOOL)isAddedToFavorites:(NSString*)viewID {
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *favourites = [[standardUserDefaults objectForKey:kFavouriteItemsKey] mutableCopy];

if(favourites && [[favourites objectForKey:kFavouriteItemsKey] objectForKey:viewID])
return YES;

return NO;
}

-(void)addToFavourites:(NSString*)viewID viewType:(NSString*)viewType {
NSMutableDictionary *myMutableDictionary= [NSMutableDictionary dictionaryWithCapacity:[myDictionary count]+1];
[myMutableDictionary addEntriesFromDictionary:myDictionary];
[myMutableDictionary setObject:myObject forKey:myKey];


// Modified dictionary is not getting synced
[[NSUserDefaults standardUserDefaults]  setObject:myMutableDictionary  forKey:kFavouriteItemsKey];
[[NSUserDefaults standardUserDefaults]  synchronize];
}

Tried encoding dictionary data to NSData and it worked now after minor but serious corrections!

-(BOOL)isAddedToFavorites:(NSString*)viewID {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:kFavouriteItemsKey];
NSDictionary *favourites = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 

if(favourites && [favourites objectForKey:viewID])
    return YES;

return NO;
}

-(void)addToFavourites:(NSString*)viewID viewType:(NSString*)viewType {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:kFavouriteItemsKey];
NSDictionary *myDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 

NSMutableDictionary *myMutableDictionary= [NSMutableDictionary dictionaryWithCapacity:[myDictionary count]+1];
[myMutableDictionary addEntriesFromDictionary:myDictionary];
[myMutableDictionary setObject:viewType forKey:viewID];


NSData *newdata = [NSKeyedArchiver archivedDataWithRootObject:myMutableDictionary];
[[NSUserDefaults standardUserDefaults] setObject:newdata forKey:kFavouriteItemsKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}

Thanks.

Community
  • 1
  • 1
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139
  • 1
    This http://stackoverflow.com/questions/471830/why-nsuserdefaults-failed-to-save-nsmutabledictionary-in-iphone-sdk seems to be a duplicate. Does it help? – phi Jul 06 '11 at 14:44
  • This actually looks different AppleDeveloper is storing strings and still having an issue. – Joe Jul 06 '11 at 15:01
  • @Irene: I tried encoding dictionary values but still it doesn't work! See edited question! – Paresh Masani Jul 06 '11 at 15:20
  • 1
    in encoding dictionary use [[NSUserDefaults standardUserDefaults] synchronize]; } – Dileep Reghu Jul 06 '11 at 15:37
  • OMG, it worked! Two things was my mistake: First after encoding I didn't synchronize the NSData. Second I was storing ViewType as a Key but checking for ViewID! Corrected them and it worked. – Paresh Masani Jul 06 '11 at 15:43

1 Answers1

2

You will need to implement this for the elements in your dictionary:

The element in the dictionary implements

@interface CommentItem : NSObject<NSCoding> {
    NSString *value;
}

Then in the implementation of CommentItem, provides two methods:

-(void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:value forKey:@"Value"];
}

-(id)initWithCoder:(NSCoder *)decoder
{
    self.value = [decoder decodeObjectForKey:@"Value"];
    return self;
}

As suggested on the link @Irene provided.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • Hi OscarMk, that was the things I didn't understand. I don't know how and when these methods will be inovked? It worked for me without it not sure why? I have small issue when trying to add second key. My XCode is getting hung! Not sure if it is because of this but trying to resolve it. See my complete code in edited question. Thanks. – Paresh Masani Jul 06 '11 at 15:49
  • 1
    @AppleDeveloper you would only need the above code if the objects in the nsdictionary didn't already implement NSCoding, but if it worked then they are implementing it. Those methods would get called for the NSKeyedArchiver to unarchive and archive. – Oscar Gomez Jul 06 '11 at 15:51
  • Fantastic! Thank you very much OscarMk for helping me all the way! It is working absolutely fine now. XCode editor was hung might be due to some other reason. – Paresh Masani Jul 06 '11 at 16:00