ios – 尝试插入非属性值Objective C

嗨,我正在尝试从餐厅对象创建一个四列表,我的应用程序有不同的餐馆列表,我想要用户添加好消息的能力,而这段代码不工作

- (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’。请注意,属性列表中的字典和数组也必须只包含属性值。

如果您不想担心确保所有字典值都是属性列表类型,那么您可以简单地将字典转换为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

其他注意事项:如果您收到可变字典或可变数组,请记住使用“mutableCopy”方法。否则,您无法从检索到的字典/数组中添加或删除对象。例如:

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

要么

NSMutableArray *retrievedArray = 
[[NSKeyedUnarchiver unarchiveObjectWithData:data] mutableCopy];
http://stackoverflow.com/questions/15857408/attempt-to-insert-non-property-value-objective-c

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:ios – 尝试插入非属性值Objective C