MappableObject 0.3.12

MappableObject 0.3.12

测试测试
语言语言 SwiftSwift
许可 MIT
发布上次发布2018年4月
SwiftSwift 版本3.0
SPM支持 SPM

Arnaud Dorgans 维护。



 
依赖关系
RealmSwift~> 3.0
ObjectMapper~> 3.0
 

  • Arnoymous

MappableObject

CI Status Version License Platform

示例

要运行示例项目,请克隆仓库,然后首先从 Example 目录中运行 pod install

需求

安装

MappableObject 通过 CocoaPods 提供。要安装它,只需在 Podfile 中添加以下行

pod "MappableObject"

使用

不含主键的对象

class Cat: MappableObject {
    
    dynamic var name: String = ""
    
    override func mapping(map: Map) {
        super.mapping(map: map)
        
        name <- map["name"]
    }
}

含有主键的对象

class Dog: MappableObject {
    
    dynamic var name: String = ""
    dynamic var age: Int = 0
    
    override class func primaryKey() -> String? {
        return "name"
    }
    
    override func mappingPrimaryKey(map: Map) {
        name <- map["name"]
    }
    
    override func mapping(map: Map) {
        super.mapping(map: map)
        
        age <- map["age"]
    }
}

JSON中主键不同的对象

class Person: MappableObject {
    
    dynamic var name: String = ""
    let dogs = List<Dog>()
    
    override class func primaryKey() -> String? {
        return "name"
    }
    
    override class func jsonPrimaryKey() -> String? {
        return "full_name"
    }
    
    override func mappingPrimaryKey(map: Map) {
        name <- map["full_name"]
    }
    
    override func mapping(map: Map) {
        super.mapping(map: map)
        
        dogs <- map["dogs"]
    }
}

自定义MapContext

class CustomContext: RealmMapContext {
    
    var customValue: String
    
    init(customValue: String) {
        self.customValue = customValue
        super.init()
    }
}

映射器

映射

let person = Mapper<Person>().map(JSON: ["full_name": "Arnaud Dorgans"])

映射与数据库同步

let person = Mapper<Person>(options: .sync).map(JSON: ["full_name": "Arnaud Dorgans"])
//get or create Person in DB with 'Arnaud Dorgans' primary key

映射同步并复制到数据库

let person = Mapper<Person>(options: [.sync, .copy]).map(JSON: ["full_name": "Arnaud Dorgans"]) 
//get Person from DB with 'Arnaud Dorgans' primary key, or create Person from JSON detached from Realm
//and then update detached object from JSON

更新

let mapper = Mapper<Person>(options: .sync)
try! Realm().write{
   var person = mapper.map(JSON: ["full_name": "Arnaud Dorgans", "dogs": [["name": "Sansa", "age": 1]]])!
   // get or create Person in DB with 'Arnaud Dorgans' primary key & update 'dogs' property
   person = mapper.map(JSON: ["full_name": "Arnaud Dorgans"], toObject: person)
   //dogs won't be updated  cause 'dogs' key isn't provided
   person = mapper.map(JSON: ["full_name": "Arnaud Dorgans", "dogs": []], toObject: person)
   //dogs will be updated cause 'dogs' key is provided
}

覆盖

let realm = try! Realm()
try! realm.write{
   var person = mapper.map(JSON: ["full_name": "Arnaud Dorgans", "dogs": [["name": "Sansa", "age": 1]]])!
   // get or create Person in DB with 'Arnaud Dorgans' primary key & update 'dogs' property

   person = Mapper<Person>(options: [.sync, .override]).map(JSON: ["full_name": "Arnaud Dorgans"])! 
   //dogs will be reset to default value [] cause 'dogs' key isn't provided
}

作者

匿名,[email protected]

许可证

MappableObject 在 MIT 许可证下可用。更多信息请参阅 LICENSE 文件。