Skip to content

Instantly share code, notes, and snippets.

View zntfdr's full-sized avatar
🌙
BRB GOING TO THE MOON

zntfdr

🌙
BRB GOING TO THE MOON
View GitHub Profile
struct Connection {
let to: Node
let weight: Int
init(to toNode: Node, weight: Int) {
assert(weight >= 0, "connection weight has to be equal or greater than zero")
self.to = toNode
self.weight = weight
}
struct Node {
let name: String
var visited: Bool = false
var connections: [Connection] = []
init(name: String) {
self.name = name
}
}
let json = JSON(data: dataFromServer)
if let model = MyModel.obtainModel(from: json) {
// success
} else {
// failure
}
// Some model
struct MyModel {
let name: String
let number: Int
let date: NSDate
}
// Conforming to the new protocol
extension MyModel: JSONParser {
static func obtainModel(from json: JSON) -> MyModel? {
protocol JSONParser {
static func obtainModel(from json: JSON) -> Self?
}