NSStructMapper
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Usage
Step 1: Create identical classes
// Same property names along with interchangeable data types between swift and objc
// MARK: - Swift Address
struct Address {
var city: String?
var zipCode: String?
}
// MARK: - Swift Equivalent ObjC Address
@objc class NSAddress: NSObject {
var city: String?
var zipCode: String?
}
Step 2: Conform your data models
// Conform your swift structs with Mappable
struct Contact: Mappable
// Conform your NSObjects with NSMappable
@objc class NSContact: NSObject, NSMappable
Step 3: Implement Methods
// Conform your swift structs with Mappable
struct Contact: Mappable {
// Your properties
// Write the following code in Mappable, result should be type of NSObject you want to convert in
func nsObject() throws -> Storable {
let result: NSContact = try conversionLogic()
return result
}
}
// Conform your NSObjects with NSMappable
@objc class NSContact: NSObject, NSMappable {
// Your properties
// Write the following code in NSMappable, result should be type of SwiftObject you want to convert in
func swiftObject() throws -> Mappable {
let result: Contact = try conversionLogic()
return result
}
}
Step 4: Enjoy
// Convert your SwiftObject to NSObject
let nsContact: NSContact = try! contact.nsObject() as! NSContact
// Convert your NSObject to SwiftObject
let sob: Contact = try! nsContact.swiftObject() as! Contact
Installation
NSStructMapper is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'NSStructMapper'
Author
License
NSStructMapper is available under the MIT license. See the LICENSE file for more info.