Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Swift Optional without Syntax Sugar

484 views

Published on

“?”1文字の裏に隠されている本当のコードを示すことで、Swift言語のオプショナルとオプショナルにまつわる糖衣構文が何をしているのかを明らかにします。

Published in: Software
  • DOWNLOAD THI5 BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download Full EPUB Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download EPUB Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download doc Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download Full EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download Full EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download Full EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download doc Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download Full EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Swift Optional without Syntax Sugar

  1. 1. 糖衣構文なしで Swi$のオプショナルを 使うとどうなるか? 2016年1月9日 藤本尚邦 / Cocoa勉強会(関東) #76 1
  2. 2. 自己紹介 • 藤本尚邦 (@%isa) • h+ps://github.com/%isa • フリーランスプログラマー • 主な作品: RubyCocoa, Swi=yLayout • Mac開発歴、薄く長く約25年 • iOS開発歴、約1年 2
  3. 3. Agenda • オプショナルおさらい • 糖衣構文なしでオプショナル • オプショナル使いこなしの注意点 • まとめ 3
  4. 4. オプショナルおさらい 普通の型に"?"または"!"の1文字を付けて型宣言す るとオプショナルになります。見た目はたった1文 字の違いですが、普通の型とオプショナル型はまっ たくの別物です。 var absolutelyInt: Int // 普通の型 var maybeInt: Int? // オプショナル型 var probablyInt: Int! // 暗黙のオプショナル型 4
  5. 5. オプショナルおさらい オプショナルはおおよそ以下のように定義された普 通の enum です1 : enum Optional<Wrapped> { case None case Some(Wrapped) } 1 この発表で不要な情報は省いています 5
  6. 6. オプショナルおさらい オプショナルが他のenumで定義された型と違うのは 豊富な糖衣構文2 を持っている。この一点です。 var maybeArray: [Int]? // Optional Type var probablyArray: [Int]! // Implicitly Unwrapped Optional Type if let array = maybeArray { ... } else { ... } // Optional Binding let x = maybeArray?.count // Optional Chaining let x = maybeArray! // Forced Unwrapping let x = probablyArray.count // Implicitly Forced Unwrapping let x = maybeArray ?? [1,2,3] // Nil Coalescing Operator let x = someObject as? [Int] // Type Cast Operator let x = someObject as! [Int] // Forced Type Cast Operator 2 英語では Syntax Sugar あるいは Syntac-c Sugar 6
  7. 7. 糖衣構文なしでオプショナル • 変数の宣言 • Op$onal Binding • Op$onal Chaining • Forced Unwrapping • Nil Coalescing Operator 7
  8. 8. 変数の宣言 var maybeInt: Int? var maybeArray: [Int]? 糖衣構文なしだと var maybeInt: Optional<Int> var maybeArray: Optional<Array<Int>> 8
  9. 9. Op#onal Binding if let array = maybeArray { IF-CLAUSE } else { ELSE-CLAUSE } 糖衣構文なしだと switch maybeArray { case .Some(let array): IF-CLAUSE case .None: ELSE-CLAUSE } 9
  10. 10. Op#onal Chaining let x = maybeArray?.count // Optional Chaining 糖衣構文なしだと let x = ({ Void -> Optional<Int> in switch maybeArray { case .None: return .None case .Some(let array): return array.count } })() 10
  11. 11. Forced Unwrapping let x = maybeArray! // Forced Unwrapping 糖衣構文なしだと let x = ({ Void -> Int in switch maybeArray { case .None: fatalError("unexpectedly found nil ...") case .Some(let array): return array } })() 11
  12. 12. Nil Coalescing Operator let x = maybeArray ?? [1,2,3] // Nil Coalescing Operator 糖衣構文なしだと let x = ({ (arg:[Int]) -> [Int] in switch maybeArray { case .None: return arg case .Some(let array): return array } })([1, 2, 3]) 12
  13. 13. オプショナル使いこなしの注意点 • "?"付きで型宣言するのが基本 • "!"付き型宣言は明確な理由がなければ使わない • 強制アンラップ・キャストは、値がnilならバグの ときのみ使う • "?"や"??"を積極的に使うとコードの可読性が増す if let delegate = delegate { delegate.someMethod() } // 冗長 delegate?.someMethod() // 簡潔・可読性良し 13
  14. 14. まとめ • 普通の型とオプショナル型は見た目以上に異なる • オプショナルはenumで定義された単なる型 • Swi%プログラミングではオプショナルが重要 • 糖衣構文なしでのオプショナルのプログラムは地獄 • だからたくさん糖衣構文があるんだよ • オプショナルをきちんと理解して良いSwi%プログラム を書こう 14
  15. 15. 参考文献 • The Swi) Programming Language (Swi) 2.1) h8ps://developer.apple.com/library/ios/ documentaDon/Swi)/Conceptual/ Swi)ProgrammingLanguage/ 15
  16. 16. Thank you!2016年1月9日 藤本尚邦 / Cocoa勉強会(関東) #76 16

×