简体   繁体   English

Swift(String:AnyObject)无法转换为[String:AnyObject]

[英]Swift (String:AnyObject) is not convertible to [String:AnyObject]

Hi I got a code like this but the Xcode show an error: 嗨,我得到了这样的代码,但是Xcode显示一个错误:

(String:AnyObject) is not convertible to [String:AnyObject] (String:AnyObject)不能转换为[String:AnyObject]

func personDetails (dic : [String:AnyObject]) -> Array<Person> {
       for personDic: [String : AnyObject] in dict { >> error in this line
        let person = Person.init(person: personDic)
        //rest the code
        }
}

How to make it correct? 如何使其正确?

The problem is that, you trying iterate for loop and in for loop you want every iteration to be a Dictionary from a Dictionary that is not possible. 问题是,你想迭代for循环和for loop ,你想每次迭代是一个DictionaryDictionary这是不可能的。 You can write for loop with Dictionary like this. 您可以像这样用Dictionary编写for循环。

for(key,value) in dict {
    print(key)
    print(value)
}

Here key contain key and value contains its corresponding value for that key. 在此,键包含键,值包含其对应的值。 Using this loop you can iterate to every key->value pair of dictionary. 使用此循环,您可以迭代字典的每个key->value对。

What you should do is probably following: 您应该做的可能是以下几点:

for (key, value) in dict {
    let person = Person(person: [key: value])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM