Created
May 13, 2018 12:42
-
-
Save GeekTree0101/87e3b199e9ec24fd075b300d069fb735 to your computer and use it in GitHub Desktop.
Test Mergeable Protocol
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
class User: NSObject, Decodable, Mergeable { | |
@objc var username: String = "" | |
@objc var profileURL: URL? | |
enum CodingKeys: String, CodingKey { | |
case username = "login" | |
case profileURL = "avatar_url" | |
} | |
} | |
let mock = [ | |
[ | |
"login": "Geektree0101", | |
"avatar_url": "helloworld" | |
], | |
[ | |
"login": "Rinat", | |
"avatar_url": "BoloTarara" | |
] | |
] | |
if let data = try? JSONSerialization.data(withJSONObject: mock, options: .prettyPrinted), | |
let userList = try? JSONDecoder().decode([User].self, from: data) { | |
let firstUser = userList[0] | |
let lastUser = userList[1] | |
print("name: \(lastUser.username)") // Rinat | |
lastUser.merge(firstUser) | |
print("name: \(lastUser.username)") // Geektree0101 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment