Skip to content

Commit

Permalink
Adding init for object mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Jan 26, 2017
1 parent 74f83f1 commit 983e5a1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion EVReflection.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "EVReflection"
s.version = "4.0.10"
s.version = "4.1.0"
s.summary = "Reflection based (dictionary, JSON or XML) object mapping (including extensions for Alamofire and Moya with RxSwift or ReactiveSwift)"

s.description = <<-EOS
Expand Down
Binary file not shown.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ func testArrayFunctions() {
let newDictionaryArray = userArray.toDictionaryArray()
}
func testMapping() {
let player = GamePlayer()
player.name = "It's Me"
let administrator = GameAdministrator(usingValuesFrom: player)
}
```

This is how you can parse a .plist into an object model. See EVReflectionIssue124.swift to see it working.
Expand Down
14 changes: 14 additions & 0 deletions Source/EVReflectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ extension EVReflectable where Self: NSObject {
}
}

/**
Convenience init for creating an object whith the property values of an other object.

- parameter usingValuesFrom: The object of whicht the values will be used to create this object
- parameter conversionOptions: Option set for the various conversion options.
*/
public init(usingValuesFrom: EVReflectable, conversionOptions: ConversionOptions = .None) {
self.init()
let dict = usingValuesFrom.toDictionary()
let _ = EVReflection.setPropertiesfromDictionary(dict, anyObject: self, conversionOptions: conversionOptions)
}



/**
Returns the hashvalue of this object

Expand Down
3 changes: 3 additions & 0 deletions UnitTests/EVReflectionTests/EVReflectionMappingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ class EVReflectionMappingTests: XCTestCase {
NSLog("\n\n===> This will output a warning because GameAdministrator does not have the propery gamesPlayed")
let administrator: GameAdministrator = player.mapObjectTo()

let administrator2 = GameAdministrator(usingValuesFrom: player)

// Remember that printing will use the property converter and multiply the administrator level with 4. So it will print the same as the player.
NSLog("player = \(player)")
NSLog("administrator = \(administrator)")
NSLog("administrator2 = \(administrator2)")

XCTAssertEqual(administrator.name, player.name, "The names should be the same")
XCTAssertEqual(administrator.memberSince, player.memberSince, "The member since dates should be the same.")
Expand Down

0 comments on commit 983e5a1

Please sign in to comment.