Created
May 13, 2018 12:39
-
-
Save GeekTree0101/aa956977da31d1e44109265122140f25 to your computer and use it in GitHub Desktop.
Mergeable Protocol Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol Mergeable { | |
func merge(_ target: Self) | |
} | |
extension Mergeable { | |
func merge(_ target: Self) { | |
guard let source = self as? NSObject else { return } | |
let mirrorSource = Mirror(reflecting: source) | |
let mirrorTarget = Mirror(reflecting: target) | |
for sourceItem in mirrorSource.children { | |
for targetItem in mirrorTarget.children { | |
if let targetLabel = targetItem.label, | |
sourceItem.label == targetLabel { | |
source.setValue(targetItem.value, forKey: targetLabel) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment