Skip to content

Latest commit

 

History

History
122 lines (88 loc) · 2.74 KB

README.md

File metadata and controls

122 lines (88 loc) · 2.74 KB

FlexibleModelProtocolImage

FlexibleModelProtocol

Model protocol with flexible, diverse types of conversion

Platform Cocoapods Compatible

Requirements

  • iOS 11.0+ / macOS 10.13+ / tvOS 13.0+ / watchOS 6.0+
  • Xcode 11+
  • Swift 5.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate FlexibleModelProtocol into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'FlexibleModelProtocol', '~> 1.1'

Usage

example model

import FlexibleModelProtocol

struct MyModel: FlexibleModelProtocol {
    typealias selfType = MyModel
    ...
}

From Data

  • JSON
let myModel: MyModel? = MyModel.fromJson(jsonData: data)
  • XML
let myModel: MyModel? = MyModel.fromXML(xmlData: data)
  • Dictionary
let myModel: MyModel? = MyModel.fromDictionary(dictionary: data)
  • NSDictionary
let myModel: MyModel? = MyModel.fromNSDictionary(nsDictionary: data)

To Data

  • JSON
let jsonString: String = myModel.toJson()
  • XML
let xmlString: String? = myModel.toXML()
  • Dictionary
let dictionary: [String:Any]? = myModel.toDictionary()
  • NSDictionary
let nsDictionary: NSDictionary? = myModel.toNSDictionary()

Copy on Write

example model

struct MyModel: FlexibleModelProtocol {
    ...
}
struct MyCopyOnWriteModel: CopyOnWriteModelProtocol {
    typealias ModelType = MyModel
    var dataWrapper: DataWrapper<MyModel>? = nil
}

FlexibleModelProtocolModel to CopyOnWriteModelProtocolModel

let model: MyModel = MyModel(...)
var emptyCowObj: MyCopyOnWriteModel = MyCopyOnWriteModel()
let cowData: MyCopyOnWriteModel? = model.toCopyOnWriteModel(object: &emptyCowObj)

CopyOnWriteModelProtocolModel to FlexibleModelProtocolModel

let cowModel: MyCopyOnWriteModel = CowModel(dataWrapper: DataWrapper(originModel: MyModel(...)))
let flexibleModel: MyModel? = cowModel.toFlexibleProtocolModel()

* Sample Project *

I wrote an example of how to use it for the unit test.

Contact Us

Email : [email protected]

License

FlexibleModelProtocol is released under the MIT license. See LICENSE for details.