ios 尝试插入非属性值 Objective C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15857408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:04:36  来源:igfitidea点击:

Attempt to insert non-property value Objective C

iosobjective-cnsmutablearraynsuserdefaultsproperty-list

提问by mavusane

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

嗨,我正在尝试从餐馆对象创建一个 Fourates 列表,我的应用程序有一个不同餐馆的列表,我希望用户能够添加喜欢的餐馆,但此代码不起作用

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

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

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

回答by GeneCode

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,

如果您不想费心确保所有字典值都是属性列表类型,那么您可以简单地将字典转换为 NSData,

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

And to get back into a dictionary from sandbox:

并从沙箱返回字典:

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

Hope this helps someone. :D

希望这可以帮助某人。:D



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:

附加说明:如果您正在获取可变字典或可变数组,请记住使用“mutableCopy”方法。否则,您无法从检索到的字典/数组中添加或删除对象。例如:

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

Or

或者

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

回答by Catfish_Man

You can only store property list types (array, data, string, number, date, dictionary) or urls in NSUserDefaults. You'll need to convert your model object to those.

您只能在 NSUserDefaults 中存储属性列表类型(数组、数据、字符串、数字、日期、字典)或 url。您需要将模型对象转换为这些对象。