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

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

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

AKIBA.swiftでの発表資料です

Takaaki Tanaka

May 30, 2016
Tweet

More Decks by Takaaki Tanaka

Other Decks in Technology

Transcript

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

    J04%FWFMPQFS 4XJGU0CKFDUJWF$  w (ZB[4RVBSF(JU)VC
  2. &OVN typedef NS_OPTIONS(NSUInteger, IMType) { IMTypeCute = 1UL << 0,

    IMTypeCool = 1UL << 1, IMTypePassion = 1UL << 2, IMTypeAll = (IMTypeCute | IMTypeCool | IMTypePassion) }; w 0CKFDUJWF$ $PDPB 4UZMFɹ
  3. 3BX7BMVF enum PeachBoySlave: Int { case Dog case Monkey case

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

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

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

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

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

    count: Int { switch self { case Oni: return 10 case Boss: return 1 } } } w ܭࢉܕϓϩύςΟͷΈఆٛ͢Δ͜ͱ͕Ͱ͖Δ
  9. 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Λࢦఆ͢Δ͜ͱͰҟͳΔߏ଄Λ࣋ͭ ϝϯόʔΛఆٛ͢Δ͜ͱ͕Ͱ͖Δ
  10. "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 } }
  11. "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 } }
  12. δΣωϦΫεͷԠ༻ public enum Result<Value, Error: ErrorType> { case Success(Value) case

    Failure(Error) } w UVCMFͱδΣωϦΫεΛࢦఆ͢Δ͜ͱͰ੒ޭ࣌ɺࣦ ഊ࣌΋஋Λฦ͢&OVNΛఆٛ͢Δ͜ͱ͕Ͱ͖Δ
  13. δΣωϦΫεͷԠ༻ 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ʹͨ͠৔߹
  14. δΣωϦΫεͷԠ༻ let result = resultWithCode(200) switch result { case .Success(let

    code): print(code) case .Failure(let error): print(error) } w ஋͸ύλʔϯϚονͰൺֱ͢Δ͜ͱ͕Ͱ͖Δ
  15. ࣮ࡍʹ࢖͏έʔε 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() }