22
SWIFT & JSON John Sundell Lead iOS Developer, Spotify @johnsundell SWIFT

Swift & JSON

Embed Size (px)

Citation preview

Page 1: Swift & JSON

SWIFT & JSONJohn Sundell

Lead iOS Developer, Spotify

@johnsundellSWIFT

Page 2: Swift & JSON

SWIFT =❤

Page 3: Swift & JSON

SWIFT =+ JSON "

Page 4: Swift & JSON

struct User { let name: String let age: Int }

Page 5: Swift & JSON

{ "name": "John", "age": 28 }

Page 6: Swift & JSON

func userFromData(data: NSData) throws -> User { let object = try NSJSONSerialization.JSONObjectWithData(data, options: []) guard let dictionary = object as? [String : AnyObject] else { throw JSONError.InvalidJSONObject } guard let name = dictionary["name"] as? String else { throw JSONError.InvalidValueForKey("name") } guard let age = dictionary["age"] as? Int else { throw JSONError.InvalidValueForKey("age") } return User(name: name, age: age) }

NOT REUSABLE

Page 7: Swift & JSON

TO THE GITHUB MOBILE!

Page 8: Swift & JSON

let json = JSON(data: data)

guard let name = json["name"].string else { throw JSONError.InvalidJSONObject }

guard let age = json["age"].string else { throw JSONError.InvalidJSONObject }

return User(name: name, age: age)

Page 9: Swift & JSON

extension User: Decodable { static func decode(j: JSON) -> Decoded<User> { return curry(User.init) <^> j <| "id" <*> j <| "name" <*> j <|? "email" <*> j <| "role" <*> j <| ["company", "name"] <*> j <|| “friends" } }

Page 10: Swift & JSON

class User: Mappable { var username: String? var age: Int? var weight: Double! var array: [AnyObject]? var dictionary: [String : AnyObject] = [:] var bestFriend: User? var friends: [User]? var birthday: NSDate? required init?(_ map: Map) {} func mapping(map: Map) { username <- map["username"] age <- map["age"] weight <- map["weight"] array <- map["arr"] dictionary <- map["dict"] bestFriend <- map["best_friend"] friends <- map["friends"] birthday <- (map["birthday"], DateTransform()) } }

Page 11: Swift & JSON

EASY TO WRITE EASY TO READ

CLEAN MODEL CODE

Page 12: Swift & JSON

UNBOXTHE EASY TO USE SWIFT JSON DECODER

Page 13: Swift & JSON

struct User let name: String let age: Int

}

{: Unboxable

init(unboxer: Unboxer) { self.name = unboxer.unbox("name") self.age = unboxer.unbox("age") }

Page 14: Swift & JSON

let user: User = try Unbox(data)

Page 15: Swift & JSON

TYPE INFERENCE

Page 16: Swift & JSON

METHOD OVERLOADING

Page 17: Swift & JSON

class Unboxer {func unbox(key: String) -> Stringfunc unbox(key: String) -> Intfunc unbox(key: String) -> Double

func unbox(key: String) -> String?func unbox(key: String) -> Int?func unbox(key: String) -> Double?

... } ... }

Page 18: Swift & JSON

struct User let name: String let age: Int

}

{: Unboxable

init(unboxer: Unboxer) { self.name = unboxer.unbox("name") self.age = unboxer.unbox("age") }

?

Page 19: Swift & JSON

UNBOXER HOLDS STATE SO THAT OUR MODELS DON’T HAVE TO.

Page 20: Swift & JSON

DEMO

Page 21: Swift & JSON

TRANSFORMATIONS

CUSTOM UNBOXING API

ARRAY UNBOXINGALLOWING INVALID

COLLECTION ELEMENTS

CUSTOMIZED WRAPPING

PROPERTY TO KEY MAPPINGUNBOXABLE KEYS

DATE & URL SUPPORT

CONTEXTUAL OBJECTS

WRAPPABLE KEYS

Page 22: Swift & JSON

SUNDELL.CO/UNBOX

SUNDELL.CO/WRAP

@JOHNSUNDELL