OMM 0.10.0

OMM 0.10.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2019 年 4 月
SPM支持 SPM

Ivan Nikitin 维护。



OMM 0.10.0

  • Ivan Nikitin

OMM GitHub MIT License GitHub Release Carthage compatible CocoaPods Version CocoaPods License CocoaPods Platform

OMM是一个帮助将 JSON 和属性列表对象映射到 Swift 实例的 one more mapper。

特性

首先,OMM 只提供从 JSON 到单向转换。对于双向转换,请尝试其他库,如 SwiftyJSONObjectMapper 或任何其他的。

OMM 还提供了一个从属性列表到单向转换。

OMM 不会扩展任何标准类型。它允许保持清晰紧凑的 API。

OMM 支持Swift错误处理。

OMM 提供了索引语法,可以从对象和数组中获取值。

OMM 允许区分值不存在和值错误。

OMM 提供使用可映射类型和自定义转换重用映射的机会。

快速开始

初始化

import OMM

let jsonDataNode = makeNode(forJSON: someData)
let propertyListDataNode = makeNode(forPropertyList: anotherData)
let anyObjectNode = makeNode(for: anyObject)

索引

let nodeForArrayElement = node[42]
let nodeForPopertyValue = node["propertyName"]
let nodeForMixedPath = node["property", "anotherProperty", 0]

实体化

let intValue = try node.value(Int.self)
let arrayOfStrings = try node.array(String.self)

错误处理

do {
    let intValue: Int = try node.value()
    // intValue contains non-optional integer value
} catch let error as MappingError {
    // There is no value or value is not number
} catch {
    // Actually, never happens
}

可选

do {
    let intValue = try node.optional?.value(Int.self)
    // intValue contains optional integer value
} catch {
    // Value is not number
}

let stringValue = try? node.value(String.self)
// stringValue contains optional string value anyway

转换

let urlValue = try node.value(transformedWith: URLTransform())

struct LengthTransform: Transform {
    func apply(to node: Node) throws -> Double {
        let value = try node.value(Double.self)
        if value < 0 {
            throw error(reason: "Length should be non-negative")
        }
        return value
    }
}
let length = try node.value(transformedWith: LengthTransform())

可映射类型

struct User: Mappable {
    let identifier: Int64
    let name: String

    init(node: Node) throws {
        identifier = try node["user_id"].required.value(transformedWith: Int64Transform)
        name = try node["user_name"].optional?.value(String.self) ?? ""
    }
}
let user = try node.value(User)

许可证

OMM遵循MIT许可协议发布。

安装

pod 'OMM'

# to get property list support as well
pod 'OMM/PropertyList'
github "fen-x/OMM"
  1. 仅支持JSON时使用OMM4JSON.framework
  2. 支持属性列表时使用OMM4PropertyList.framework
  3. 同时支持JSON和属性列表时使用OMM.framework

不要忘记为导入声明使用适当的模块名称:OMM4JSONOMM4PropertyListOMM