Skip to content

Instantly share code, notes, and snippets.

@alickbass
Created March 15, 2017 15:57
Show Gist options
  • Save alickbass/66c99a7d862292ef5c4898c975fe27b4 to your computer and use it in GitHub Desktop.
Save alickbass/66c99a7d862292ef5c4898c975fe27b4 to your computer and use it in GitHub Desktop.
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)
@retrokid
Copy link

this looks cool dude thx :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment