Skip to content

Instantly share code, notes, and snippets.

@gonzo-oin
Last active November 8, 2017 10:51
Show Gist options
  • Save gonzo-oin/ad4afdfa37ce65991ace74dadba45944 to your computer and use it in GitHub Desktop.
Save gonzo-oin/ad4afdfa37ce65991ace74dadba45944 to your computer and use it in GitHub Desktop.
Swift Duplicatable protocol: Duplicate class object that implement Codable.
import Foundation
protocol Duplicatable {}
extension Duplicatable where Self: Codable {
func duplicate() throws -> Self {
return try JSONDecoder().decode(Self.self, from: JSONEncoder().encode(self))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment