iPhone:无法将 NSMutableDictionary 存储到 NSUserDefaults

发布于 2024-11-18 21:09:43 字数 2570 浏览 2 评论 0原文

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

编辑: 从这个我开始知道我必须在可变和不可变字典中移动数据,然后它就可以正常工作了!但仍然无法将修改后的 NSMutableDictionary 同步到 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,经过微小但严重的更正后现在可以工作了!

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

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷默言语 2024-11-25 21:09:43

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

字典中的元素实现

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

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

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

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

正如提供的链接 @Irene 所建议的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文