iPhone:无法将NSMutableDictionary存储到NSUserDefaults [英] iPhone: Not able to store NSMutableDictionary to NSUserDefaults

查看:109
本文介绍了iPhone:无法将NSMutableDictionary存储到NSUserDefaults的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NSMutableDictionary实现添加到收藏夹功能,因为我必须添加多个键值。即使将对象添加到字典后,以下addToFavourites方法也始终显示Count = 0。我阅读这个并将nsdefaults变为mutabledictionary(而不是可变数组)并设置结果字典回到nsdefaults但不知道是什么问题。任何帮助将非常感激。谢谢。

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.

编辑:
来自这个我知道我必须在可变和不可变的字典周围移动数据然后它工作正常!但仍无法将修改后的NSMutableDictionary与NSUserDefaults同步。

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];
}

尝试将字典数据编码到NSData,现在它经过了轻微但严重的修正后工作了!

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];
}

谢谢。

推荐答案

您需要为字典中的元素实现此功能:

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

字典中的元素实现

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

然后在CommentItem的实现中,提供了两种方法:

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;
}

正如@Irene提供的链接所示。

As suggested on the link @Irene provided.

这篇关于iPhone:无法将NSMutableDictionary存储到NSUserDefaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆