Skip to content

Instantly share code, notes, and snippets.

@GeekTree0101
Created May 13, 2018 12:39
Show Gist options
  • Save GeekTree0101/aa956977da31d1e44109265122140f25 to your computer and use it in GitHub Desktop.
Save GeekTree0101/aa956977da31d1e44109265122140f25 to your computer and use it in GitHub Desktop.
Mergeable Protocol Example
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