Alamofire ObjectMapper - 从所有模型中提取公共字段并映射它们

时间:2018-05-07 06:26:01

标签: ios json swift alamofire objectmapper

api返回的所有模型中都很少有字段。但它们并不是一个单独的对象。他们在其他领域。

两个模型的示例:

活动:

[  
   {  
      "event":{  
         "id":3,
         "company_id":18,
         "archived":false,
         "created_by":229,
         "updated_by":229,
         "owner_id":229,
         "subject":"",
         "start_date":null,
         "end_date":null,
         "name":null,
         "name_class_name":"",
         "related_to":null,
         "related_to_class_name":"",
         "status":"",
         "created_at":"2018-05-07T01:59:38.921-04:00",
         "updated_at":"2018-05-07T01:59:38.921-04:00",
         "custom_nf":false
      }
   }
]  

机会:

[  
   {  
      "opportunity":{  
         "id":4,
         "company_id":18,
         "archived":false,
         "created_by":229,
         "updated_by":229,
         "owner_id":229,
         "account_id":null,
         "name":"",
         "lead_source":"",
         "amount":null,
         "close_date":null,
         "probability":null,
         "stage":"",
         "created_at":"2018-05-07T01:49:55.441-04:00",
         "updated_at":"2018-05-07T01:49:55.441-04:00"
      }
   }
]

如图所示,初始字段在两个(所有)模型中都很常见 - id,company_id,archived,created_by 等。
我使用 ObjectMapper 进行了大量项目,但之前没有遇到过这种情况。我完全了解处理嵌套模型,但这是一个不同的情况 虽然我可以通过重复所有模型中的所有常见字段来轻松处理此问题。 但这听起来不太好。 我正在寻找一种方法,我可以创建一个包含所有常见字段的单独模型类。但问题是 - 如何使用ObjectMapper将其与api响应进行映射?

举个例子,这就是我创建机会模型的方式:

import UIKit
import ObjectMapper

class Opportunity: NSObject, Mappable {

    var id: Int?
    var companyId: Int?
    var archived: Int?
    var createdBy: Int?
    var updatedBy: Int?
    var ownerId: Int?
    var accountId: Int?
    var name: String?
    var leadSource: String?
    var amount: String?
    var closeDate: String?
    var probability: String?
    var stage: String?
    var createdAt: String?
    var updatedAt: String?

    required init?(map: Map) {

    }

     func mapping(map: Map) {
        self.id <- map["id"]
        self.companyId <- map["company_id"]
        self.archived <- map["archived"]
        self.createdBy <- map["created_by"]
        self.updatedBy <- map["updated_by"]
        self.ownerId <- map["owner_id"]
        self.accountId <- map["account_id"]
        self.name <- map["name"]
        self.leadSource <- map["lead_source"]
        self.amount <- map["amount"]
        self.closeDate <- map["close_date"]
        self.probability <- map["probability"]
        self.stage <- map["stage"]
        self.createdAt <- map["created_at"]
        self.updatedAt <- map["updated_at"]
    }
}

1 个答案:

答案 0 :(得分:5)

您可以创建基本实体并放置公共字段。

示例:

import UIKit
import ObjectMapper

class BaseEntity: NSObject, Mappable {

    var id: Int?
    var companyId: Int?
    var archived: Int?
    var createdBy: Int?
    var updatedBy: Int?
    var ownerId: Int?
    var name: String?
    var createdAt: String?
    var updatedAt: String?

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        self.id <- map["id"]
        self.companyId <- map["company_id"]
        self.archived <- map["archived"]
        self.createdBy <- map["created_by"]
        self.updatedBy <- map["updated_by"]
        self.ownerId <- map["owner_id"]
        self.name <- map["name"]
        self.createdAt <- map["created_at"]
        self.updatedAt <- map["updated_at"]
    }

}

class Opportunity: BaseEntity {

    var accountId: Int?
    var leadSource: String?
    var amount: String?
    var closeDate: String?
    var probability: String?
    var stage: String?

    required init?(map: Map) {
        super.init(map: map)
    }

    override func mapping(map: Map) {
        super.mapping(map: map)
        self.accountId <- map["account_id"]
        self.leadSource <- map["lead_source"]
        self.amount <- map["amount"]
        self.closeDate <- map["close_date"]
        self.probability <- map["probability"]
        self.stage <- map["stage"]
    }

}

注意: BaseEntity不是一个好名字,我想你可以给出更好的名字。