尝试插入非属性值Objective C [英] Attempt to insert non-property value Objective C

查看:139
本文介绍了尝试插入非属性值Objective C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试从餐馆对象创建一个游戏列表,我的应用程序有一个不同的餐馆列表,我希望能够为用户添加有利的餐馆,这个代码不起作用

Hi l am trying to create a fourates lists from an restaurants Object, my application has a list of different restaurants, l want the ability for users to add favourate restaurants, and this code is not working

- (IBAction)toggleFav:(id)sender {

    Restaurant *resto = [self restaure];

    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:resto.price forKey:@"restoPrice"];
    [dic setObject:resto.restaurantId forKey:@"restaurantId"];
    [dic setObject:resto.restoAbout forKey:@"restoAbout"];
    [dic setObject:resto.restoAddress forKey:@"restoAddress"];
    [dic setObject:resto.restoBeverages forKey:@"restoBeverages"];
    [dic setObject:resto.restoCategory forKey:@"restoCategory"];
    [dic setObject:resto.restoEmail forKey:@"restoEmail"];
    [dic setObject:resto.restoLogo forKey:@"restoLogo"];
    [dic setObject:resto.restoName forKey:@"restoName"];
    [dic setObject:resto.restoPhone forKey:@"restoPhone"];
    [dic setObject:resto.restoCity forKey:@"restoCity"];

    NSArray *dicArray = [dic allKeys];


    if([sender isSelected]){
        //...
        [sender setSelected:NO];
        NSMutableArray *array = [[[NSUserDefaults standardUserDefaults] objectForKey:@"restoName"] mutableCopy];
        [array removeObject:dicArray];
        [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"restoName"];

    } else {
        //...
        [sender setSelected:YES];
        NSMutableArray *array = [[[NSUserDefaults standardUserDefaults] objectForKey:@"restoName"] mutableCopy];
        [array addObject:dicArray];
        [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"restoName"];
        [[NSUserDefaults standardUserDefaults] synchronize];

       //NSLog(@"%@",[[NSUserDefaults standardUserDefaults] stringForKey:@"restoName"]);
  }
}

班级'__ NSCFArray'的

'。请注意,属性列表中的字典和数组也必须只包含属性值。

' of class '__NSCFArray'. Note that dictionaries and arrays in property lists must also contain only property values.

推荐答案

如果您不想打扰确保所有字典值都是属性列表类型,然后你可以简单地将字典转换为NSData,

If you don't want to bother about ensuring all your dictionary values are the property list types, then you can simply convert the dictionary into NSData,

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:[NSKeyedArchiver archivedDataWithRootObject:self.myDictionary] forKey:@"MyData"];
[def synchronize];

要从沙箱返回字典:

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSData *data = [def objectForKey:@"MyData"];
NSDictionary *retrievedDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];
self.myDictionary = [[NSDictionary alloc] initWithDictionary:retrievedDictionary];

希望这有助于某人。 :D

Hope this helps someone. :D

附加说明:如果您要获得可变字典或可变数组,然后记得使用mutableCopy方法。否则,您无法从检索到的字典/数组中添加或删除对象。例如:

ADDITIONAL NOTE: If you are getting a mutable dictionary, or mutable array, then remember to use "mutableCopy" method. Otherwise you can't add or delete objects from your retrieved dictionary/array. For example:

NSMutableDictionary *retrievedDictionary = 
[[NSKeyedUnarchiver unarchiveObjectWithData:data] mutableCopy];

NSMutableArray *retrievedArray = 
[[NSKeyedUnarchiver unarchiveObjectWithData:data] mutableCopy];

这篇关于尝试插入非属性值Objective C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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