变异方法发送到不可变对象'

时间:2016-07-14 09:05:20

标签: ios objective-c objective-c-blocks

我正在尝试从字典中向数组添加对象。在其他部分我收到此错误

  

发送到不可变对象的变异方法'

NSMutableDictionary *selectedDict = [NSMutableDictionary new];
    [selectedDict setObject:editedLineItem forKey:@"Text"];
    [selectedDict setObject:@"fa-check" forKey:@"IconClass"];
    NSMutableArray *tagListDictionary = [NSMutableArray new];
    [tagListDictionary addObject:selectedTagsArray];
    LineItemsStorage *linestorage = [LineItemsStorage sharedManager];
    if(![linestorage.packagesArray valueForKey:@"Id"])
    {
        [linestorage.selectedLineItemsAndTagsArray addObject:selectedDict];
    }
    else{        [[linestorage.packagesArray valueForKey:@"LineItems"]addObject:[NSMutableArray arrayWithObject:selectedDict]];
    }
  

- [ NSCFArray insertObject:atIndex:]:发送到不可变对象的变异方法'   ***第一次抛出调用堆栈:   (       0 CoreFoundation 0x00000001154a1d85 __exceptionPreprocess + 165       1 libobjc.A.dylib 0x0000000114f15deb objc_exception_throw + 48       2 CoreFoundation 0x00000001154a1cbd + [NSException raise:format:] + 205       3 CoreFoundation 0x0000000115497b0a - [__ NSCFArray insertObject:atIndex:] + 106       4 FlatPebble 0x000000010f276014 - [LineItemViewController okayAction] + 836       5 UIKit 0x0000000113809a8d - [UIApplication sendAction:to:from:forEvent:] + 92       6 UIKit 0x000000011397ce67 - [UIControl sendAction:to:forEvent:] + 67       7 UIKit 0x000000011397d143 - [UIControl _sendActionsForEvents:withEvent:] + 327       8 UIKit 0x000000011397c263 - [UIControl touchesEnded:withEvent:] + 601       9 UIKit 0x000000011387c99f - [UIWindow _sendTouchesForEvent:] + 835       10 UIKit 0x000000011387d6d4 - [UIWindow sendEvent:] + 865       11 UIKit 0x0000000113828dc6 - [UIApplication sendEvent:] + 263       12 UIKit 0x0000000113802553 _UIApplicationHandleEventQueue + 6660       13 CoreFoundation 0x00000001153c7301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17       14 CoreFoundation 0x00000001153bd22c __CFRunLoopDoSources0 + 556       15 CoreFoundation 0x00000001153bc6e3 __CFRunLoopRun + 867       16 CoreFoundation 0x00000001153bc0f8 CFRunLoopRunSpecific + 488       17 GraphicsServices 0x0000000116e5cad2 GSEventRunModal + 161       18 UIKit 0x0000000113807f09 UIApplicationMain + 171       19 *********** 0x000000010f348c2f main + 111       20 libdyld.dylib 0x0000000115d9992d start + 1   )

2 个答案:

答案 0 :(得分:0)

使用此代码

 NSMutableDictionary *selectedDict = [[NSMutableDictionary new]mutableCopy];
    [selectedDict setObject:editedLineItem forKey:@"Text"];
    [selectedDict setObject:@"fa-check" forKey:@"IconClass"];
    NSMutableArray *tagListDictionary = [[NSMutableArray new]mutableCopy];
    [tagListDictionary addObject:selectedTagsArray];
    LineItemsStorage *linestorage = [LineItemsStorage sharedManager];
    if(![linestorage.packagesArray valueForKey:@"Id"])
    {
        [linestorage.selectedLineItemsAndTagsArray addObject:selectedDict];
    }
    else{        [[linestorage.packagesArray valueForKey:@"LineItems"]addObject:[NSMutableArray arrayWithObject:selectedDict]];
    }

答案 1 :(得分:0)

假设您的LineItems包含一个数组,该数组包含一个或多个具有[[linestorage.packagesArray valueForKey:@"LineItems"]addObject:[NSMutableArray arrayWithObject:selectedDict]]属性的对象(恰好也是一个数组);您的问题在这里:

NSArray* packagesLineItems = [linestorage.packagesArray valueForKey:@"LineItems"]; NSMutableArray* selected = [NSMutableArray arrayWithObject:selectedDict]; [packagesLineItems addObject:selected];

打破它相当于:

education_bachelor_summary<- education_bachelor %>%
  group_by(title_mapped) %>%
  summarise(n=n()) %>%
  mutate(perc = n / sum(n)) %>%
  arrange(desc(n)) %>%
  mutate(label=(str_c(title_mapped, "-", as.character(percent(perc)), sep=" ")))


midpoint <-sum(education_bachelor_summary$perc) - cumsum(education_bachelor_summary$perc) + education_bachelor_summary$perc/2

ggplot(education_bachelor_summary, aes(x = "", y=perc, fill = factor(title_mapped))) +
  geom_bar(width = 1, stat = "identity") +
  scale_y_continuous(breaks=midpoint, labels=education_bachelor_summary$label) +
  scale_fill_brewer(palette = "Blues", direction = -1) +
  labs(fill="Bachelor/ Vordiplom",x=NULL,y=NULL,title="",caption="") +
  coord_polar(theta = "y", start=0) +
  theme(axis.ticks = element_blank(), panel.grid  = element_blank(), axis.line = element_blank(), strip.background = element_blank(), panel.background = element_blank())

所以您的问题是valueForKey的返回类型(在数组上调用时),或者您要对其执行的操作。