gpt4 book ai didi

ios - Xcode 8 iOS 10 - NSMutableDictionary 抛出 - "mutating method sent to immutable object"

转载 作者:行者123 更新时间:2023-11-29 00:39:30 25 4
gpt4 key购买 nike

我有一个 NSMutable 字典,我预加载它来编辑保存的估计(保存在 userDefaults 中)。 eParts 存储在另一个名为“estimate”的 NSMutableDictionary 中,其分割如下所示:

if ([appDefaults objectForKey:@"selectedEstimate"]) {
estimate = [[NSMutableDictionary alloc]initWithDictionary:[[appDefaults objectForKey:@"selectedEstimate"] mutableCopy] copyItems:YES];
eParts = [[NSMutableDictionary alloc] initWithDictionary:[[estimate objectForKey:@"eParts"] mutableCopy] copyItems:YES];
eRniParts = [[NSMutableDictionary alloc]initWithDictionary:[[estimate objectForKey:@"eRniParts"] mutableCopy] copyItems:YES];
matrix = [[estimate objectForKey:@"matrix_id"] intValue];
company = [[db getBodyshop:[[estimate objectForKey:@"company_id"] intValue]] mutableCopy];
vehicle = [[estimate objectForKey:@"vehicle"] mutableCopy];
otherParts = [[NSMutableArray alloc] initWithArray:[estimate objectForKey:@"otherParts"] copyItems:YES];
NSLog(@"other parts now contains: %@",otherParts);
} else {
estimate = [[NSMutableDictionary alloc] init];
eParts = [[NSMutableDictionary alloc] init];
eMiscParts = [[NSMutableDictionary alloc] init];
eRniParts = [[NSMutableDictionary alloc] init];
otherParts = [[NSMutableArray alloc] init];
matrix = [[appDefaults objectForKey:@"selectedMatrix"] intValue];
company = [[NSMutableDictionary alloc] initWithDictionary:[[appDefaults objectForKey:@"autobodyCompany"] mutableCopy] copyItems:YES];
vehicle = [[NSDictionary alloc] initWithDictionary:[[appDefaults objectForKey:@"vehicle"] mutableCopy] copyItems:YES];
[estimate setObject:[vehicle mutableCopy] forKey:@"vehicle"];
}


Printing description of self->estimate:
{
address = "1818 University Ave NE";
city = "Somewhere Else";
"company_id" = 1;
"company_name" = "Abco Autobody";
"contact_name" = "Mike Peterson";
date = "10-04-2016";
eParts = {
Liftgate = {
bodyshop = 0;
"bs_price" = "Body Shop";
"dent_size" = DIME;
"entry_id" = 6;
"estimate_id" = 5;
"off_matrix" = 0;
"off_matrix_price" = "(null)";
oversized = 1;
"oversized_quantity" = "";
"part_name" = Liftgate;
quantity = "1-5";
type = NRML;
};
};
email = "mikep@abcoautobody.com";
"estimate_id" = 5;
"grand_total" = "95.00";
invoiced = 0;
"matrix_id" = 0;
paid = 0;
phone = "651-555-4321";
state = MN;
vehicle = {
agency = "";
"claim_number" = "";
"estimate_id" = 5;
make = chevrolet;
model = "silverado 1500";
plate = "";
"repair_order_number" = "";
submodel = "double cab";
"vehicle_id" = 6;
vin = 1GCVKREC4FZ221600;
year = 2015;
};
zip = 55421;
}


eParts = [[NSMutableDictionary alloc] initWithDictionary:[[[appDefaults objectForKey:@"estimate"] objectForKey:@"eParts"] mutableCopy] copyItems:YES];

然后当我尝试从可变字典中删除该项目时:

    [[estimate objectForKey:@"eParts"] removeObjectForKey:tempPartName];

它抛出:

    *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
*** First throw call stack:
(0x184a281c0 0x18346055c 0x184a28108 0x184913a5c 0x10005e3ec 0x100062188 0x10005d05c 0x18a89f7b0 0x18a89f730 0x18a889be4 0x18a89f01c 0x18ae2ff30 0x18ae2bfc8 0x18ae2bae8 0x18ae2ada4 0x18a899d70 0x18a86a858 0x18b057cb8 0x18b051720 0x1849d6278 0x1849d5bc0 0x1849d37c0 0x184902048 0x186385198 0x18a8d5628 0x18a8d0360 0x10007d00c 0x1838e45b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

在 Xcode 8 和 iOS 10 之前这不是问题...任何帮助将不胜感激!

提前致谢。

最佳答案

试试这个

实际上,您可以通过 deepCopy 进行转换。

if ([appDefaults objectForKey:@"selectedEstimate"]) {

estimate = [[NSMutableDictionary alloc]initWithDictionary:[[appDefaults objectForKey:@"selectedEstimate"] mutableCopy] copyItems:YES];
estimate = (NSMutableDictionary *)CFBridgingRelease(CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFDictionaryRef)estimate, kCFPropertyListMutableContainers));

eParts = [estimate objectForKey:@"eParts"];
eRniParts = [estimate objectForKey:@"eRniParts"];
matrix = [[estimate objectForKey:@"matrix_id"] intValue];
company = [[db getBodyshop:[[estimate objectForKey:@"company_id"] intValue]] mutableCopy];
vehicle = [estimate objectForKey:@"vehicle"];
otherParts = [estimate objectForKey:@"otherParts"];
NSLog(@"other parts now contains: %@",otherParts);

} else {
estimate = [[NSMutableDictionary alloc] init];
eParts = [[NSMutableDictionary alloc] init];
eMiscParts = [[NSMutableDictionary alloc] init];
eRniParts = [[NSMutableDictionary alloc] init];
otherParts = [[NSMutableArray alloc] init];
matrix = [[appDefaults objectForKey:@"selectedMatrix"] intValue];
company = [[NSMutableDictionary alloc] initWithDictionary:[[appDefaults objectForKey:@"autobodyCompany"] mutableCopy] copyItems:YES];
vehicle = [[NSDictionary alloc] initWithDictionary:[[appDefaults objectForKey:@"vehicle"] mutableCopy] copyItems:YES];
[estimate setObject:[vehicle mutableCopy] forKey:@"vehicle"];
}

之后让我知道它是否有效!!

希望对你有所帮助。

编码愉快!!

关于ios - Xcode 8 iOS 10 - NSMutableDictionary 抛出 - "mutating method sent to immutable object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853557/

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