Cannot convert value of type 'JSON' to expected argument type 'JSON'

Harsha picture Harsha · Apr 21, 2016 · Viewed 9.3k times · Source

Below is the code,

//Controller.swift
struct MyObject {
    init(myJson: JSON) {
    // some code here
    }
}

//unittest.swift
let json = JSON("{}")
let obj = MyObject(myJson: json)

I get the error Cannot convert value of type 'JSON' to expected argument type 'JSON'

Note: However, this does not happen when I put the struct in the same unit test file. Using SwiftyJSON library for JSON parsing

Answer

Stanley Yong picture Stanley Yong · May 19, 2017

I got this error when I forgot to convert from JSON to Swift representation of the data type. Hope this example helps.

For example:

let someString = json["name"].string let someArray = json["products"].arrayObject