Slide 1

Slide 1 text

Swi$%Β͍͠%API%ΫϥΠΞϯτ !"APIKit"Λ࢖͏"! Ӊࠤݟ!ެี!@usamik26

Slide 2

Slide 2 text

Ӊࠤݟ!ެี!@usamik26 • ϑΣϯϦϧגࣜձࣾ"iOS"ΞϓϦϓϩάϥϚ • WWDC"2015"ࢀՃ • Objec1ve3C"޷͖ • Swi5"࠷ۙ޷͖ʹͳͬͨ • Haskell"ษڧத

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

APIKit

Slide 5

Slide 5 text

APIKit • Swi%&Ͱ&Web&API&ΫϥΠΞϯτ࣮૷Λ͢ΔͨΊͷϥΠϒϥϦ • h.ps:/ /github.com/ishkawa/APIKit • 3&݄ͷ&potato>ps&Ͱ͝ຊਓʹΑΔ঺հ • ͦͷޙɺ࢓༷มߋˍόʔδϣϯΞοϓ • ࣮Ҋ݅Ͱ࠾༻͠·ͨ͠

Slide 6

Slide 6 text

APIKit'Λ࢖ͬͨίʔυ

Slide 7

Slide 7 text

API$Λ࢖͏ଆͷίʔυ let request = HogeApi.Endpoint.Login(user: "hoge", pass: "hoge") HogeApi.sendRequest(request) { response in switch response { case .Success(let box): let loginResponse = box.value // ਖ਼ৗϨεϙϯε ... case .Failure(let box): let error = box.value // NSError ...

Slide 8

Slide 8 text

API$Λ࣮૷͢Δଆͷίʔυ$(1) class HogeApi: API { override class var baseURL: NSURL { return NSURL(string: "https://hogehoge/api")! } class Endpoint { // ͜ͷதʹ֤ΤϯυϙΠϯτͷΫϥεΛ࣮૷͠·͢ // ࠓճ͸ผϑΝΠϧʹ෼͚ͯ extension Ͱ࣮૷͠·ͨ͠ } }

Slide 9

Slide 9 text

API$Λ࣮૷͢Δଆͷίʔυ$(2) extension HogeApi.Endpoint { class Login: Request { let user: String let pass: String var URLRequest: NSURLRequest? { return HogeApi.URLRequest(method: .GET, path: "login", parameters: ["user": user, "pass": pass]) } typealias Response = LoginResponse static func responseFromObject(object: AnyObject) -> Response? { return LoginResponse(object) } ... }

Slide 10

Slide 10 text

API$Λ࣮૷͢Δଆͷίʔυ$(3) extension HogeApi.Endpoint { ... class LoginResponse { let json: JSON // SwiftyJSON let token: String init (_ object: AnyObject) { let json = JSON(object) self.json = json self.token = json["token"].stringValue } } }

Slide 11

Slide 11 text

APIKit'ͷར఺

Slide 12

Slide 12 text

ैདྷͷύλʔϯ • API%ͷ%comple,on%handler%ʹɺresponse%ͱ%error%͕྆ํ͋Δ • response%:%੒ޭ࣌ͷϨεϙϯε • error%:%ࣦഊ࣌ͷΤϥʔ

Slide 13

Slide 13 text

ैདྷύλʔϯͷ໰୊఺

Slide 14

Slide 14 text

APIKit'ͷίʔυʢ࠶ܝʣ let request = HogeApi.Endpoint.Login(user: "hoge", pass: "hoge") HogeApi.sendRequest(request) { response in switch response { case .Success(let box): let loginResponse = box.value // LoginResponse ... case .Failure(let box): let error = box.value // NSError ...

Slide 15

Slide 15 text

APIKit'ͷར఺

Slide 16

Slide 16 text

an#typical/Result/ͷ׆༻ enum Result { case Success(Box) case Failure(Box) } • response"͸"Result"ܕ • .Success"ͷͱ͖"LoginResponse"ܕ • .Failure"ͷͱ͖"NSError"ܕ

Slide 17

Slide 17 text

Designed(for(Safety

Slide 18

Slide 18 text

Designed(for(Safety • Swi%&ͷಛ௃ͷͻͱͭ • C&ݴޠ͕ʮϓϩάϥϚΛ৴༻͢Δʯࢥ૝Ͱ͋Δͷͱରরత • ྫ͑͹&Op*onal&͸͜ͷಛ௃Λ࣮ݱ͢ΔͨΊͷΩʔػೳͷͻͱͭ • Op*onal&ܕΛ&unwrap&͠ͳ͍ͱී௨ͷܕͱͯ͠ѻ͑ͳ͍ • null&ࢀর͕ͳ͘ͳͬͯ҆શ

Slide 19

Slide 19 text

APIKit'ͷར఺ • Ϩεϙϯε͕໌֬Ͱෆ໌ͳঢ়ଶ͕ͳ͍ • Swi%&Β͍͠&ʹ&Designed&for&Safety

Slide 20

Slide 20 text

·ͱΊ Swi$%Λ࢖ͬͯ҆શͳίʔυΛॻ͜͏