Created
March 15, 2017 15:57
-
-
Save alickbass/66c99a7d862292ef5c4898c975fe27b4 to your computer and use it in GitHub Desktop.
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
import SwiftyJSON | |
extension Person { | |
enum PropertyKey: String { | |
case firstName, lastName, age, isMarried, height, hobbies | |
} | |
init(json: JSON) { | |
firstName = json[PropertyKey.firstName.rawValue].stringValue | |
lastName = json[PropertyKey.lastName.rawValue].stringValue | |
age = json[PropertyKey.age.rawValue].intValue | |
isMarried = json[PropertyKey.isMarried.rawValue].boolValue | |
height = json[PropertyKey.height.rawValue].doubleValue | |
hobbies = json[PropertyKey.height.rawValue].arrayValue.map({ $0.stringValue }) | |
} | |
} | |
let data: Data // received from a network request, for example | |
let json = JSON(data: data) // creates type JSON from Data | |
let person = Person(json: json) | |
print(person) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this looks cool dude thx :)