RealmCrust 0.0.5

RealmCrust 0.0.5

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布日期最新版本2016年2月
SPM支持 SPM

Rex Fenley 维护。



 
依赖
Crust>= 0
RealmSwift>= 0
 

  • 作者
  • rexmas

RealmCrust

简单 Crust 扩展,用于将 Realm 对象从 JSON 映射。用于轻松地将 JSON/Realm 对象映射到/从对象。

需求

iOS 8.0+ Swift 2.1+

安装

Pods

platform :ios, '8.0'
use_frameworks!

pod 'Crust'
pod 'RealmCrust'

注意:RealmCrust 包括 Realm 作为依赖项,但不包括 Crust 作为依赖项。这是为了允许使用依赖于 Crust 的多个模块而不发生依赖冲突。

附加步骤

将以下代码复制到您的项目源文件中,以使用 RealmCrust 与 Crust。

/// Instructions:
/// 1. `import Crust` and `import RealmCrust` dependencies.
/// 2. Include this section of code in your app/lib.
/// This will allow our `RealmMapping` and `RealmAdaptor` to be used with Crust.

public protocol RealmMapping {
    init(adaptor: RealmAdaptor)
}
extension RealmAdaptor : Adaptor { }

public func <- <T, U: Mapping, C: MappingContext where U.MappedObject == T>(field: List<T>, map:(key: KeyExtensions<U>, context: C)) -> C {

    // Realm specifies that List "must be declared with 'let'". Seems to actually work either way in practice, but for safety
    // we're going to include a List mapper that accepts fields with a 'let' declaration and forward to our
    // `RangeReplaceableCollectionType` mapper.

    var variableList = field
    return mapField(&variableList, map: map)
}

如何使用

RealmAdaptor 遵循 AdaptorRealmMapping 遵循 Mapping。与其他任何 MappingAdaptor 一样,与 Crust 一起使用。

例如。

public class Company: Object {

    public let employees = List<Employee>()
    public dynamic var uuid: String = ""
    public dynamic var name: String = ""
    public dynamic var foundingDate: NSDate = NSDate()
    public dynamic var founder: Employee?
    public dynamic var pendingLawsuits: Int = 0
}

public class CompanyMapping : RealmMapping {

    public var adaptor: RealmAdaptor
    public var primaryKeys: Array<CRMappingKey> {
        return [ "uuid" ]
    }

    public required init(adaptor: RealmAdaptor) {
        self.adaptor = adaptor
    }

    public func mapping(inout tomap: Company, context: MappingContext) {
        let employeeMapping = EmployeeMapping(adaptor: self.adaptor)

        tomap.employees             <- .Mapping("employees", employeeMapping) >*<
        tomap.founder               <- .Mapping("founder", employeeMapping) >*<
        tomap.uuid                  <- "uuid" >*<
        tomap.name                  <- "name" >*<
        tomap.foundingDate          <- "data.founding_date"  >*<
        tomap.pendingLawsuits       <- "data.lawsuits.pending"  >*<
        context
    }
}

线程

RealmAdaptor 不是线程安全的。请确保为每个线程创建一个新的 RealmAdaptor

许可证

MIT 许可证 (MIT)

版权所有 © 2015 Rex

根据以下条件,特此授予任何获得本软件及其相关文档文件(“软件”)副本的任何人免费的使用许可,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的许可,并允许向您提供软件的人这样做:

以上版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、适用于特定用途和不受侵犯的保证。在任何情况下,作者或版权所有者都不应对任何索赔、损害或其他责任负责,无论此类索赔、损害或其他责任是否基于合同行为、侵权或其他行为,以及从、源于或与该软件或对该软件的使用或经营相关的其他事宜。