簡體   English   中英

發送給不可變對象的變異方法”

[英]mutating method sent to immutable object'

我試圖從字典添加對象到數組。 在其他部分,我得到這個錯誤

發送給不可變對象的變異方法”

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 0x00000001154NS1ccdd :: + 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 0x000000011397Control sendAction:to:forEvent:] + 67 7 UIKit 0x000000011397d143-[UIControl _sendActionsForEvents:withEvent:] + 327 8 UIKit 0x000000011397c263-[UIControl touchesEnded:withEvent:] + 601 9 UIKit 0x000000011387c99f-[UIWindow _sendTouches011Eventd] -6 UIKit 0x000000011387c99f -d [UIWindow sendEvent:] + 865 11 UIKit 0x0000000113828dc6-[UIApplication sendEvent:] + 263 12 UIKit 0x0000000113802553 _UIApplicationHandleEventQueu E + 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)

使用此代碼

 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]];
    }

假設您的packagesArray包含一個數組,該數組包含一個或多個具有LineItems屬性的對象(碰巧也是一個數組); 您的問題在這里:

[[linestorage.packagesArray valueForKey:@"LineItems"]addObject:[NSMutableArray arrayWithObject:selectedDict]]

將其分解等效於:

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

因此,您的問題是valueForKey的返回類型(在數組上調用時),或者您要對其執行的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM