1

i have a JSON as:

{
  "jsonData": {
    "userDetails": [
      {
        "user_id": "a",
        "first_name": "First1",
        "last_name": "Last1",
        "donation_amount": 841,
        "donation_time": 1452678347523
      },
      {
        "user_id": "b",
        "first_name": "First2",
        "last_name": "Last2",
        "donation_amount": 841,
        "donation_time": 1452678347523
      },
      {
        "user_id": "c",
        "first_name": "First3",
        "last_name": "Last3",
        "donation_amount": 841,
        "donation_time": 1452678347523
      }
    ]
  },
  "total_count": 3
}

and i am parsing in swift using swiftlyJSON : my code is below

    private func processProjectDonorsResponse(response: JSON) {
     //add  to  dictionary
        let jsonObject = response.dictionaryObject!["jsonData"]! as AnyObject
        let details = jsonObject["userDetails"] as! [AnyObject]
        var tempModel = [UserModel]()
        for detail in details {
          let user = UserModel(response: detail as! [String: AnyObject])
          tempModel.append(user)
        }
}

it was working fine in swift 2 , but now i have upgraded to swift 3 i am getting warning at line //let details = jsonObject["userDetails"] as! [AnyObject] as: Cast from String ?! to unrelated type '[AnyObject]' always fail., and gets crashed. how to fix this problem?

1 Answer 1

1

The problem in your code is in the following line:

let jsonObject = response.dictionaryObject!["jsonData"]! as AnyObject

Just change this code with the following:

let jsonObject = response.dictionaryObject!["jsonData"]! as! [String: AnyObject]

Hope, this will work for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.