简体   繁体   中英

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

Hi I got a code like this but the Xcode show an error:

(String:AnyObject) is not convertible to [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. You can write for loop with Dictionary like this.

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.

What you should do is probably following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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