CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

MappingAce 1.0.4

MappingAce 1.0.4

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by 11401160202oO.



  • By
  • Binglin

Swift JSON to Struct, class

MappingAce allows rapid creation of struct , Swift class, OC class . Automatic transform dictionary to model(model could be struct), forget to manually write property mapping code

Usage

JSON -> Model

Raw struct

// 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)
    
Nested struct mapping
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)
Optional property
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)
Enum

enum type of Int & String is support

// 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"
}

Struct or class has default property value

protocol: InitMapping (Struct or Class)

// 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)

Model -> 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"]

Installation

Podfile

To integrate MappingAce into your Xcode project using CocoaPods, specify it in your Podfile:

platform :ios, '8.0'

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

Then, run the following command:

$ pod install