我有一个具有这两个字段的模型
var latitude: String?
var longitude: String?
我正在尝试解析模型。一切都被正确解析,但从未解析过这两个字段。
这是完整的模型
import ObjectMapper
class BarberLoginModel: Mappable {
var id: Int?
var barberShopId: Int = 0
var firstName = ""
var lastName = ""
var mobileNo = ""
var userName = ""
var email = ""
var imgPath = ""
var shopName = ""
var address = ""
var street = ""
var area = ""
var emirates = ""
var latitude: String?
var longitude: String?
var Services: [ServiceModel]?
var Stylist: StylistArrayModel?
var Availability: [AvailabilityModel]?
var Facilities: [FacilitiesModel]?
required init?(map: Map) {
}
func mapping(map: Map) {
id <- map["id"]
barberShopId <- map["barberShopId"]
firstName <- map["firstName"]
lastName <- map["lastName"]
mobileNo <- map["mobileNo"]
userName <- map["userName"]
email <- map["email"]
imgPath <- map["imgPath"]
shopName <- map["shopName"]
address <- map["address"]
street <- map["street"]
area <- map["area"]
emirates <- map["emirates"]
latitude <- map["latitude"]
longitude <- map["longitude"]
Services <- map["Services"]
Stylist <- map["Stylist"]
Availability <- map["Availability"]
Facilities <- map["Facilities"]
}
}
注意:当我将数据类型更改为Double时?它可以工作,但由于加倍,它会增加额外的数字。
答案 0 :(得分:1)
如果没有访问实际数据,这很难调试。我在下面放了一个小测试。如果运行它,您将知道每次测试中的第二个XCTAssertEqual都会失败,因为原始JSON数据在值26.432附近没有引号。我的猜测是你的数据是一个数字而不是一个字符串,因此不会正确地解组。
import XCTest
import ObjectMapper
class Test : Mappable {
var latitude: String?
var longitude: String?
required init?(map: Map) {
}
func mapping(map: Map) {
latitude <- map["latitude"]
longitude <- map["longitude"]
}
}
class ObjectMapper_RndTests: XCTestCase {
override func setUp() {
super.setUp()
}
override func tearDown() {
super.tearDown()
}
func testExample() {
let json = "{\"longitude\": 26.432, \"latitude\":\"82.123\"}"
let test = Test(JSONString: json);
guard let t = test else {
return
}
XCTAssertEqual(t.latitude, "82.123")
XCTAssertEqual(t.longitude, "26.432")
}
func testExampleTwo() {
let json = [ "longitude": 26.432, "latitude": "82.123" ] as [String : Any]
let test = Test(JSON: json);
guard let t = test else {
return
}
XCTAssertEqual(t.latitude, "82.123")
XCTAssertEqual(t.longitude, "26.432")
}
}
如果无法将数据源更改为String,则解决方案是使用转换。 ObjectMapper网站https://github.com/Hearst-DD/ObjectMapper上有更多信息。它看起来像这样。
class Test : Mappable {
var latitude: String?
var longitude: String?
required init?(map: Map) {
}
func mapping(map: Map) {
let transform = TransformOf<String, Double>(fromJSON: { (value: Double?) -> String? in
if let v = value {
NSLog("from json value: " + String(v))
return String(v)
}
NSLog("from json value: null")
return nil
}, toJSON: { (value: String?) -> Double? in
// transform value from Double? to String?
if let value = value {
return Double(value)
}
return nil
})
latitude <- (map["latitude"], transform)
longitude <- (map["longitude"], transform)
}
}
答案 1 :(得分:0)
或者,您可以手动执行。简洁明了的代码
class BarberLoginModel: Mappable {
var latitude: Double?
var longitude: Double?
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
var latitude: String?
var longitude: String?
latitude <- map["latitude"]
longitude <- map["longitude"]
if let latitude = latitude {
self.latitude = Double(string: latitude)
}
if let longitude = longitude {
self.longitude = Double(string: longitude)
}
}
}