解析JSON并使用ObjectMapper,SwiftRealm,ObjectMapperAdditions,ObjectMapperAdditionsRealm和Alamofire

时间:2018-07-11 23:51:43

标签: json swift cocoapods alamofire objectmapper

关于,我正在尝试从placeHolder页面(用户(https://jsonplaceholder.typicode.com/个用户))序列化json,我试图一起使用以下库:

-> Alamofire

-> RealmSwift

-> ObjectMapper

-> ObjectMapperAdditions

-> ObjectMapper_Realm

这里是我正在定义的模型,但是问题是SwiftRealm不支持ArrayDictionary数据类型,因为当您尝试更改COMPANY&ADDRESS属性中的类型(属于数组类型)时,请尝试将其放入如List,[Address1] ?、 Address1,List等,但是它不起作用,它始终标记相同的错误“该操作无法完成,ObjectMapper无法序列化响应。”,因此可以作为对象,但是无法将其保存在RealmSwift中或无法执行该操作,但是您还会收到JSON或ARRAY的消息,并且也无法序列化它,是否已实现了类似的东西?

import Foundation
import RealmSwift
import ObjectMapper
import ObjectMapperAdditions
import ObjectMapper_Realm

class PlaceHolderClass: Object, Mappable {

    @objc dynamic var id = 0
    @objc dynamic var name = ""
    @objc dynamic var username = ""
    @objc dynamic var email = ""
    var address: Address1?
    @objc dynamic var phone = ""
    @objc dynamic var website = ""
    var company: Company?

    required convenience init?(map: Map) {
        self.init()
    }

    func mapping(map: Map) {

        id <- map["id"]
        name <- map["name"]
        username <- map["username"]
        email <-   map["email"]
        address <- map["address"]
        phone <- map["phone"]
        website <- map["website"]
        company <- map["company"]

    }  
}

class Address1: Object, Mappable {

     var street = List<String>()
     var suite = List<String>()
     var city = List<String>()
     var zipcode = List<String>()

    var geo: Geo?

    required convenience init?(map: Map) {
        self.init()
    }

    func mapping(map: Map) {

        street <- map["street"]
        suite <- map["suite"]
        city <- map["city"]
        zipcode <- map["zipcode"]
        geo <- map["geo"]

    }

}
class Geo: Object, Mappable {

     var lat  = List<String>()
     var lng  = List<String>()

    required convenience init?(map: Map) {
        self.init()
    }

    func mapping(map: Map) {
        lat <- map["lat"]
        lng <- map["lng"]
    }

}
class Company: Object, Mappable {

     var name  = List<String>()
     var catchPhrase  = List<String>()
     var bs  = List<String>()

    required convenience init?(map: Map) {
        self.init()
    }

    func mapping(map: Map) {

        name <- map["name"]
        catchPhrase <- map["catchPhrase"]
        bs <- map["bs"]

    }
}

我的请求代码:

func requestPlaceHolder(){

    let URL = "https://jsonplaceholder.typicode.com/users"

    Alamofire.request(URL).responseObject{ (response: DataResponse<PlaceHolderClass>) in

        switch response.result {

        case .success(let objects):

            let realm = try! Realm()

            let myPlaceHolderResponse = PlaceHolderClass(value: response.result.value)


            try! realm.write {
                realm.add(myPlaceHolderResponse, update: true)
                print("se agrego correctamente")
            }

            print(Realm.Configuration.defaultConfiguration.fileURL)


        case .failure(let error):
              print("Ocurrio el siguiente error \(error.localizedDescription)")

        }
            print(Realm.Configuration.defaultConfiguration.fileURL)
}
}

0 个答案:

没有答案