gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 08:07:08 24 4
gpt4 key购买 nike

您好,我正在尝试从餐厅对象创建一个 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"]);
}
}

'类'__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];

关于ios - 尝试插入非属性值 Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15857408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com