Skip to content

Instantly share code, notes, and snippets.

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