• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 发送到不可变对象(immutable对象)的变异方法'

[复制链接]
菜鸟教程小白 发表于 2022-12-11 16:59:18 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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:]: mutating method sent to immutable object' *** First throw call stack: ( 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 )



Best Answer-推荐答案


使用此代码

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

关于ios - 发送到不可变对象(immutable对象)的变异方法',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38369957/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap