无法分配给anyobject类型的不可变表达式 [英] cannot assign to immutable expression of type anyobject

查看:219
本文介绍了无法分配给anyobject类型的不可变表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var arr_contacts2 = [AnyObject]()

ViewDidLoad(){
for  contacts in info {

            var dict_contact = [String:AnyObject]()

            dict_contact["id"] = contacts.contact_id
            dict_contact["type"] = contacts.type
            dict_contact["value"] = contacts.value

            arr_contacts2.append(dict_contact)
        }
    }


IBAction(){
var dict_contact = arr_contacts2[0]
 dict_contact["value"] = "gg" //Cannot assign to immutable expression of type 'AnyObject?!'
}

为什么我会收到此错误? 代码有什么问题?

Why I am getting this error? What is the wrong with the code?

帮我解决这个问题.

谢谢您的时间

推荐答案

使用您的代码发布:

var dict_contact = arr_contacts2[0] // IT ASSIGNS object of type "AnyObject" to dict_contact
//compiler couldn't possibly know, that dict_contact have a dictionary value

如下更新您的代码

func IBAction(){
    guard let dict_contact = arr_contacts2[0] as? [String:AnyObject]
        else{
            return
        }
    var contactInfo  = dict_contact
    contactInfo["value"] = "gg" //Now it should work
    }

OR

OR

声明您的arr_contacts2如下

var arr_contacts2 = [[String:AnyObject]]()

这篇关于无法分配给anyobject类型的不可变表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆