MappingAce 1.0.4

MappingAce 1.0.4

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2017年9月
SwiftSwift 版本3.0
SPM支持 SPM

11401160202oO 维护。



  • 作者
  • Binglin

Swift JSON 到结构体,类

MappingAce 允许快速创建结构体、Swift 类、OC 类。自动将字典转换为模型(模型可以是结构体),无需手动编写属性映射代码

用法

JSON -> 模型

原始结构体

// It is recommend to implement protocol `Mapping`, and just implement `Mapping`, no more works
struct PhoneNumber: Mapping{
	var tel: String
	var type: String
}

let phoneInfo: [String : Any] = [
	"tel": "186xxxxxxxx",
    "type": "work"
]
let phone = PhoneNumber(fromDic: phoneInfo)

print(phone.tel) //"186xxxxxxxx"
print(phone.type) //"work"



// Struct did not implement the `Mapping` protocol
struct PhoneNumber {
	var tel: String
	var type: String
}

let phone = MappingAny(type: PhoneEntity.self, fromDic: phoneInfo)
    
嵌套结构体映射
struct User{
    var age: Int
    var name: String
    var phone: PhoneNumber
}

// if you want your serilized nested struct, just implement the `Mapping` protocol
struct PhoneNumber: Mapping {
    var tel: String
    var type: String
}

let dic: [String : Any] = [
    "age" : 24,
    "name": "Binglin",
    "phone": phoneInfo
]

let user = MappingAny(type: User.self, fromDic: dic)
可选属性
struct User{
    var age: Int?
    var name: String?
    var phone: PhoneNumber?
}

private struct PhoneNumber: Mapping {
    var tel: String
    var type: String
}

let dic: [String : Any] = [
    "name": "Binglin",
]

let user = MappingAny(type: User.self, fromDic: dic)

XCTAssertEqual(user.age, nil)
XCTAssertEqual(user.name, "Binglin")
XCTAssertEqual(user.phone?.tel, nil)
XCTAssertEqual(user.phone?.type, nil)
枚举

Int 和 String 类型的 enum 类型受支持

// eg: EnumInt
enum Gender: Int, EnumInt{
    case male = 1
    case female = 2
}

struct User: Mapping{
    var gender: Gender
}

let dicGender: [String : Any] = ["gender": 1]
let userMale = User(fromDic: dicGender)

XCTAssertEqual(userMale.gender, Gender.male)
// when enum is string type
enum Gender: String, EnumString{
    case male = "m"
    case female = "f"
}

结构体或类具有默认属性值

协议:InitMapping(结构体或类)

// struct
struct User: InitMapping{
    var name: String = "default"
    var age: Int?
}

let dic: [String : Any] = ["age": 14]
let user = User(fromDic: dic)

print(user.name) //"default"
print(user.age)  //14


// class
// need to implement an empty initializer.
class User: NSObject, InitMapping{
    var name: String = "default"
    var age: Int?

    required override init() {}/*required*/
}

let dic: [String : Any] = ["name" : "IB"]
let user = User(fromDic: dic)

模型 -> JSON

// for object implement Mapping or InitMapping
struct PhoneNumber: Mapping {
    var tel: String
    var type: String
}

let phone = PhoneNumber(tel: "186xxxxxxxx", type: "work")
let toDic = phone.toDictionary()
print(toDic) // ["type": "work", "tel": "186xxxxxxxx"]


// for object do not implement Mapping or InitMapping
// just implement protocol Serializable
struct PhoneNumber: `Serializable` {
    var tel: String
    var type: String
}

let phone = PhoneNumber(tel: "186xxxxxxxx", type: "work")
let toDic = phone.toDictionary()
print(toDic) // ["type": "work", "tel": "186xxxxxxxx"]

安装

Podfile

要使用 CocoaPods 将 MappingAce 集成到您的 Xcode 项目中,请指定它。

platform :ios, '8.0'

target 'TargetName' do
pod 'MappingAce', '~> 1.0.3'
end

然后,运行以下命令

$ pod install