2016-10-04 59 views
0

我有一個NSMutable字典,我預先編輯保存的估計值(保存在userDefaults中)。 EPARTS存儲在另一個的NSMutableDictionary稱爲「估計」,它的詳細情況如下:Xcode 8 iOS 10 - 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 = "[email protected]"; 
    "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之前不是問題......任何幫助都將不勝感激!

在此先感謝。

+3

您初始化「eParts」,但嘗試刪除「估計值」。 – Eimantas

+2

你如何創建/檢索估計字典? –

+0

我已更新帖子以包含「估計」信息 –

回答

4

剛剛嘗試這一點

其實,你可以將它使用與轉換做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"]; 
    } 

之後,讓我知道它的工作與否!

希望它能幫助你。

快樂編碼!

+0

This Worked ...添加此行:估計=(NSMutableDictionary *)CFBridgingRelease(CFPropertyListCreateDeepCopy(kCFAllocatorDefault,(CFDictionaryRef)estimate,kCFPropertyListMutableContainers)); ... 謝謝! –

+0

@CrypTech工作室最受歡迎..你應該提高我的答案,以及;) –

0

轉換你的對象[估計objectForKey:@ 「EPARTS」]中的NSMutableDictionary,然後嘗試刪除關鍵tempPartName

相關問題