TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jan 2018 |
SwiftSwift Version | 4.0 |
SPMSupports SPM | ✓ |
Maintained by incetro.
Transformer is a framework written in Swift that makes it easy for you to convert your database models to plain objects.
To support mapping, a class or struct just needs to implement the Mappable
protocol which includes the following functions:
init(with resolver: Resolver) throws
class CategoryPlainObject: Mappable {
let id: Int64
let name: String
var positions: [PositionPlainObject] = [] // Array of objects
required init(with resolver: Resolver) throws {
self.id = try resolver.value("id")
self.name = try resolver.value("name")
self.positions = try resolver.value("positions")
}
}
class PositionPlainObject: Mappable {
let id: Int64
let name: String
let price: Double
var category: CategoryPlainObject? = nil // Nested object
required init(with resolver: Resolver) throws {
self.id = try resolver.value("id")
self.name = try resolver.value("name")
self.price = try resolver.value("price")
self.category = try? resolver.value("category")
}
}
struct User: Mappable {
let id: Int64
let name: String
init(with resolver: Resolver) throws {
self.id = try resolver.value("id")
self.name = try resolver.value("name")
}
}
/// A simple protocol for a nice exmaple
protocol Storable: class {
static var entityName: String { get }
}
extension Storable where Self: NSManagedObject {
static var entityName: String {
return NSStringFromClass(self).components(separatedBy: ".").last ?? ""
}
init(in context: NSManagedObjectContext) {
guard let entity = NSEntityDescription.entity(forEntityName: Self.entityName, in: context) else {
fatalError("Cannot create entity for entity name: \(self.entityName)")
}
self.init(entity: entity, insertInto: context)
}
}
class CategoryModelObject: NSManagedObject, Storable {
@NSManaged var id: Int64
@NSManaged var name: String
@NSManaged var positions: NSSet
}
class PositionModelObject: NSManagedObject, Storable {
@NSManaged var id: Int64
@NSManaged var name: String
@NSManaged var price: Double
@NSManaged var category: CategoryModelObject
}
let categoryModelObject = CategoryModelObject(in: context)
let positionModelObject = PositionModelObject(in: context)
/// Fill some data to categoryModelObject & positionModelObject
...
/// And mapping from CoreData objects
let transformer = Transformer(from: .coredata)
let categoryPlainObject: CategoryPlainObject = try transformer.transform(from: categoryModelObject)
let positionPlainObject: PositionPlainObject = try transformer.transform(from: positionModelObject)
If you prefer not to use any dependency managers, you can integrate Transformer into your project manually.
Open up Terminal, cd
into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:
$ git init
Add Transformer as a git submodule by running the following command:
$ git submodule add https://github.com/incetro/Transformer.git
Open the new Transformer
folder, and drag the Transformer.xcodeproj
into the Project Navigator of your application's Xcode project.
It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.
Select the Transformer.xcodeproj
in the Project Navigator and verify the deployment target matches that of your application target.
Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
In the tab bar at the top of that window, open the "General" panel.
Click on the +
button under the "Embedded Binaries" section.
You will see two different Transformer.xcodeproj
folders each with two different versions of the Transformer.framework
nested inside a Products
folder.
It does not matter which
Products
folder you choose from, but it does matter whether you choose the top or bottomTransformer.framework
.
Select the top Transformer.framework
for iOS and the bottom one for OS X.
You can verify which one you selected by inspecting the build log for your project. The build target for
Transformer
will be listed as eitherTransformer iOS
,Transformer macOS
,Transformer tvOS
orTransformer watchOS
.
And that's it!
The
Transformer.framework
is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.
incetro, [email protected]
Transformer is available under the MIT license. See the LICENSE file for more info.