Skip to content

Instantly share code, notes, and snippets.

@jrBordet
Last active October 31, 2018 08:23
Show Gist options
  • Save jrBordet/e6d5bb07f892af89b8ce4ddf8ade23e4 to your computer and use it in GitHub Desktop.
Save jrBordet/e6d5bb07f892af89b8ce4ddf8ade23e4 to your computer and use it in GitHub Desktop.
A playground to show an example of Protocol Oriented Programming
// 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