Last active
October 31, 2018 08:23
-
-
Save jrBordet/e6d5bb07f892af89b8ce4ddf8ade23e4 to your computer and use it in GitHub Desktop.
A playground to show an example of Protocol Oriented Programming
This file contains 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
// MARK: - Protocols | |
protocol Person { | |
var name: String { get } | |
} | |
protocol User { | |
var identifier: String { get } | |
var email: String { get } | |
} | |
extension User { | |
var identifier: String { return "default value"} | |
} | |
// MARK: - Models | |
struct MyUser: Decodable, Person, User { | |
var email: String | |
var identifier: String | |
var name: String | |
} | |
let json = """ | |
{ | |
"name": "jean", | |
"identifier": "identifier", | |
"email": "[email protected]" | |
} | |
""" | |
let data = json.data(using: .utf8) | |
if let result = try? JSONDecoder().decode(MyUser.self, from: data!) { | |
dump(result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment