Casting AnyObject to Dictionary in swift

前端 未结 1 714
轻奢々
轻奢々 2020-12-24 10:50

I\'m getting data from iTunes API with AFNetworking and I want to create a Dictionary with the response but I can\'t do it.

Error: Cannot convert the expression\'s t

1条回答
  •  感情败类
    2020-12-24 11:47

    When you define a Dictionary in Swift you have to give key and value types as well. Something like:

    var jsonResult = responseObject as Dictionary
    

    If the cast fails, however, you'll get a runtime error -- you're better off with something like:

    if let jsonResult = responseObject as? Dictionary {
        // do whatever with jsonResult
    }
    

    0 讨论(0)
提交回复
热议问题