Closed coderflower closed 5 years ago
支持的哈,谢谢提供测试用例,稍等我加到手册上去
struct User: Convertible {
let id: String = ""
let nickName: String = ""
}
struct Goods: Convertible {
// 测试表明,在真机release模式下,对数字类型的let限制比较严格
// 值虽然修改成功了(可以打印Goods结构体发现price已经改掉了),但是get出来还是0.0
// 所以建议使用private(set) var取代let
private(set) var price: CGFloat = 0.0
let name: String = ""
}
struct NetResponse<Element>: Convertible {
let data: Element? = nil
let msg: String = ""
private(set) var code: Int = 0
}
let json1 = """
{
"data": {"nickName": "KaKa", "id": 213234234},
"msg": "Success",
"code" : 200
}
"""
let response1 = json1.kj.model(NetResponse<User>.self)
XCTAssert(response1?.msg == "Success")
XCTAssert(response1?.code == 200)
XCTAssert(response1?.data?.nickName == "KaKa")
XCTAssert(response1?.data?.id == "213234234")
let json2 = """
{
"data": [
{"price": "6199", "name": "iPhone XR"},
{"price": "8199", "name": "iPhone XS"},
{"price": "9099", "name": "iPhone Max"}
],
"msg": "Success",
"code" : 200
}
"""
let response2 = json2.kj.model(NetResponse<[Goods]>.self)
XCTAssert(response2?.msg == "Success")
XCTAssert(response2?.code == 200)
XCTAssert(response2?.data?.count == 3)
XCTAssert(response2?.data?[0].price == 6199)
XCTAssert(response2?.data?[0].name == "iPhone XR")
XCTAssert(response2?.data?[1].price == 8199)
XCTAssert(response2?.data?[1].name == "iPhone XS")
XCTAssert(response2?.data?[2].price == 9099)
XCTAssert(response2?.data?[2].name == "iPhone Max")
手册已经补上了
👍
比如后台返回的所有的数据都是
[data: [String: Any], msg: String, code: Int]
不知道是否支持以下这种解析