Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Enumはデキる子 ~ case .Success(let value): ~

Enumはデキる子 ~ case .Success(let value): ~

AKIBA.swiftでの発表資料です

808ce3d41280c085f8bdc27dce7ea8fb?s=128

Takaaki Tanaka

May 30, 2016
Tweet

Transcript

  1. &OVN͸σΩΔࢠ ",*#"TXJGU dDBTF4VDDFTT MFUWBMVF d 5BLBBLJ5BOBLB

  2. "CPVU.F w ాத޹໌ 5BLBBLJ5BOBLB  w Ϋϥεϝιουגࣜձࣾ w !LPOHNJOHUSBQ w

    J04%FWFMPQFS 4XJGU0CKFDUJWF$  w (ZB[4RVBSF(JU)VC
  3. "HFOEB w &OVNʹ͍ͭͯ w &OVNʹ͍ͭͯ w &OVNʹ͍ͭͯ

  4. &OVN w ϓϩάϥϜ্ɺҙຯͷ͋Δ஋Λྻڍͯ͠ఆٛ͢Δ͜ ͱ͕Ͱ͖Δ w 0CKFDUJWF$ɹˠɹ4XJGUͰେ෯ʹڧԽ͞Εͨ w Ή͠Ζ΄΅ผ෺ͱݴͬͯ΋աݴͰ͸ͳ͍

  5. &OVN enum { IMTypeCute, IMTypeCool, IMTypePassion }; w $$ 4UZMFɹ

  6. &OVN typedef NS_ENUM(NSUInteger, IMType) { IMTypeCute, IMTypeCool, IMTypePassion }; w

    0CKFDUJWF$ $PDPB 4UZMFɹ
  7. &OVN typedef NS_OPTIONS(NSUInteger, IMType) { IMTypeCute = 1UL << 0,

    IMTypeCool = 1UL << 1, IMTypePassion = 1UL << 2, IMTypeAll = (IMTypeCute | IMTypeCool | IMTypePassion) }; w 0CKFDUJWF$ $PDPB 4UZMFɹ
  8. 4XJGUͰͷ&OVNͷಛ௃ w ୈҰڃΦϒδΣΫτʢpSTUDMBTTUZQFʣ w 3BX7BMVFʢ4USJOH*OU'MPBUʣ w ωετఆ͕ٛͰ͖Δ w ಠࣗͷϝιουΛఆٛͰ͖Δ w

    $BTFʹUVQMF͕࢖͑Δ
  9. ୈҰڃΦϒδΣΫτ ʢpSTUDMBTTUZQFʣ w ม਺ʹ֨ೲՄೳ w σʔλߏ଄ʹ֨ೲՄೳ w ͦΕࣗମ͕ಠࣗʹଘࡏͰ͖Δ w ؔ਺ͷύϥϝʔλͱͯ͠౉͢͜ͱ͕Ͱ͖Δ

    w ؔ਺ͷ໭Γ஋ͱͯ͠ฦ͢͜ͱ͕Ͱ͖Δ
  10. 3BX7BMVF enum PeachBoySlave: Int { case Dog case Monkey case

    Bird } w 4XJGU4UZMFɹ print(PeachBoySlave.Bird.rawValue) // 2
  11. 3BX7BMVF enum PeachBoySlave: String { case Dog = "Dog" case

    Monkey = "Monkey" case Bird = "Bird" } w 4XJGU4UZMFɹ print(PeachBoySlave.Bird.rawValue) // Bird
  12. 3BX7BMVF guard let slaveType = PeachBoySlave(rawValue: "Bird") else { return

    } w 3BX7BMVFʹଘࡏ͢Ε͹&OVNΛ࣮ମԽͰ͖Δ print(slaveType) // Bird
  13. ωετ enum PeachBoySlave: String { case Dog case Monkey case

    Bird enum Actor { case Grandpa case Grandma } } w &OVNͷఆٛ಺ʹ͞Βʹ&OVNΛఆٛ͢Δ͜ͱ͕Մ ೳ
  14. ಠࣗͷϝιουఆٛ enum PeachBoyEnemy { case Oni case Boss func encount()

    { print("Enemy") } } w &OVNʹ΋ؔ਺Λఆٛ͢Δ͜ͱ͕Ͱ͖Δ
  15. ܭࢉܕϓϩύςΟఆٛ enum PeachBoyEnemy { case Oni case Boss … var

    count: Int { switch self { case Oni: return 10 case Boss: return 1 } } } w ܭࢉܕϓϩύςΟͷΈఆٛ͢Δ͜ͱ͕Ͱ͖Δ
  16. 5VQMF enum PeachBoyEnemy { case Oni(String, String) case Boss func

    encount() { switch self { case Oni(let number, let colorName): print(Number + ": Oni " + colorName) case Boss: print("Boss") } } } w $BTFʹ5VQMFΛࢦఆ͢Δ͜ͱͰҟͳΔߏ଄Λ࣋ͭ ϝϯόʔΛఆٛ͢Δ͜ͱ͕Ͱ͖Δ
  17. 5VQMF let oni: PeachBoyEnemy = .Oni("1", "Red") oni.encount() w $BTFʹ5VQMFΛࢦఆ͢Δ͜ͱͰҟͳΔߏ଄Λ࣋ͭ

    ϝϯόʔΛఆٛ͢Δ͜ͱ͕Ͱ͖Δ 1: Oni Red
  18. "MBNPpSFͷ3PVUFSͰͷԠ༻ enum Router: URLRequestConvertible { case ItemSearch([String : AnyObject]) var

    URLString: String { let baseURL = "http://shopping.yahooapis.jp/ShoppingWebService/V1/json/" switch self { case ItemSearch: return baseURL + "itemSearch" } } var Method: Alamofire.Method { switch self { case .ItemSearch: return .GET } } var URLRequest: NSMutableURLRequest { let request = NSMutableURLRequest(URL: NSURL(string: URLString)!) switch self { case .ItemSearch(let parameters): return Alamofire.ParameterEncoding.URL.encode(request, parameters: parameters).0 } }
  19. "MBNPpSFͷ3PVUFSͰͷԠ༻ enum Router: URLRequestConvertible { case ItemSearch([String : AnyObject]) …

    var URLRequest: NSMutableURLRequest { let request = NSMutableURLRequest( URL: NSURL(string: URLString)!) switch self { case .ItemSearch(let parameters): return Alamofire.ParameterEncoding. URL.encode( request, parameters: parameters).0 } }
  20. δΣωϦΫεͷԠ༻ public enum Result<Value, Error: ErrorType> { case Success(Value) case

    Failure(Error) } w UVCMFͱδΣωϦΫεΛࢦఆ͢Δ͜ͱͰ੒ޭ࣌ɺࣦ ഊ࣌΋஋Λฦ͢&OVNΛఆٛ͢Δ͜ͱ͕Ͱ͖Δ
  21. δΣωϦΫεͷԠ༻ func resultWithCode(code: Int?) -> Result<Int, NSError> { guard let

    unwrapedCode = code else { return .Failure(NSError(domain: "MyApp", code: 0, userInfo: nil)) } return .Success(unwrapedCode) } w ໭Γ஋Λ3FTVMUʹͨ͠৔߹
  22. δΣωϦΫεͷԠ༻ let result = resultWithCode(200) switch result { case .Success(let

    code): print(code) case .Failure(let error): print(error) } w ஋͸ύλʔϯϚονͰൺֱ͢Δ͜ͱ͕Ͱ͖Δ
  23. ࣮ࡍʹ࢖͏έʔε Alamofire.request(Router.ItemSearch(parameters)) .responseJSON { response in switch response.result { case

    .Success(let value) : let json = JSON(value) guard let resultSet = json["ResultSet"].dictionary else { return } guard let resultsOfOnePage = resultSet["0"]?.dictionary else { return } guard let results = resultsOfOnePage["Result"]?.dictionary else { return } self.items = results.keys.map { Item(json: results[$0]!) } case .Failure(let error) : print("error: \(error)") } self.tableView?.reloadData() self.refreshControl?.endRefreshing() }
  24. ࣮ફฤ .PEFSO$PSF%BUBCZUSZ4XJGU

  25. ·ͱΊ w 0CKFDUJWF$͔ΒͷڧԽ͞Ε͍ͯΔ w աڈͷҨ࢈Λܕ҆શͳϓϩάϥϜ΁ੜ·ΕมΘΒͤ ΔώϯτʹͳΔ w δΣωϦΫεͱԠ༻͢Δͱ஋ͷൺֱॲཧ΋؆ܿʹه ड़Ͱ͖Δ