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

Swift 中級編

Swift 中級編

1.Objective-Cを愛する人のためのSwift
2.Swift小ネタ集

562e29ba057361b2b944bd7bbd274887?s=128

Yusei Nishiyama

June 25, 2014
Tweet

Transcript

  1. Swift தڃฤ Nishiyama Yusei (@yuseinishiyama) Swift LT at Yahoo! JAPAN

    2014/6/25
  2. ࣗݾ঺հ • iOSΞϓϦέʔγϣϯ։ൃऀ • Unity΋গʑ • ࠷ۙ͸OpenCVɺػցֶशͷษڧΛ͍ͯ͠Δ͕਺ֶ͕ΞϨͳͷͰΞϨ • WWDC2014ؔ࿈͸ɺMetalΛ͸͡Ίͱͨ͠GPUपΓΛ௥͍͖͍ͬͯͨ name:Nishiyama

    Yusei twitter:@yuseinishiyamaɹ github:yuseinishiyama blog:http://yuseinishiyama.com
  3. Ϡό͍ Swift৘ใग़ਚͯ͘͠Δײ͕͋Δ ˞·ͱΊ͍͖ͯͨͩ͋Γ͕ͱ͏͍͟͝·͢ɻ
 େมॏๅ͓ͯ͠Γ·͢ɻ

  4. ͱΓ͋͑ͣυΩϡϝϯτ
 શ෦ಡΜͰχονͳ৘ใ୳ͦ͏… l4XJGU1SPHSBNNJOH-BOHVBHFz l6TJOH4XJGU8JUI$PDPBBOE0CKFDUJWF$z

  5. ಡΜͰؾ͍ͮͨ

  6. ࢲ͸Objective-C͕େ޷͖Ͱ͢

  7. Objective-CΛ
 Ѫ͢ΔਓͷͨΊͷSwift ୈҰ෦

  8. ᷿ͷ੠ • Objective-C͸௵͕͠ޮ͔ͳ͍ɻ • Objective-CͷγϯλοΫε͸ฅɻ • SwiftͰObjective-C͔Βղ์͞Εͨɻ • []←͜Ε͕ੜཧతʹແཧɻ •

    ((()(())))←͜Ε͸େ޷͖͚ͩͲͶʂ
  9. Ұํɺࢲ͸…

  10. ফ͑ͳ͍Objective-C΁ͷѪ ⾢PCKD@NTH4FOEͷ༐࢟ͱ
 $16ͷ׻ت ࠨਤ ϝοηʔδૹ৴ ಈతଋറ /&9545&1 ๛෋ͳ࣮ߦ࣌৘ใ *.1

  11. Swiftʹ
 Objective-Cͷ໘ӨΛ୳͢ຖ೔…

  12. ௒ઈ͍ͱ͓͍͠idܕ typedef struct objc_object { class isa; } *id; !

    // ͔Θ͍͍...
  13. ʮUsing Swift with Cocoa and Objective-Cʯ ΑΓ “Swift imports id

    as AnyObject.”
  14. ͍ͨʂʂ

  15. ࢖ͬͯΈΔ // ద౰ͳؔ਺Λએݴͯ͠... func someFunc() { return } ! //

    idܕ૬౰ͷΦϒδΣΫτΛએݴͯ͠... var anyObj : AnyObject ! // ίϯύΠϧ͸௨Δ͸ͣ... anyObj.someFunc()
  16. 'AnyObject' does not have a member named 'someFunc()'
 
 ίϯύΠϧͰ͖ͳ͍…

  17. ͜Μͳͷidܕ͡Όͳ͍ʂʂ

  18. υΩϡϝϯτͷαϯϓϧίʔυ var myObject: AnyObject myObject = NSDate() myObject.characterAtIndex(5) // crash,

    myObject does't respond to that method” ! // υΩϡϝϯτͰ͸࣮ߦ࣌ΤϥʔʹͳΔͱॻ͍ͯ͋Δɻ
  19. ΋͏গ͠ௐ΂ͯΈΔ • You can also call any Objective-C method and

    access any property without casting to a more specific class type. This includes Objective-C compatible methods marked with the @objc attribute. • @objcଐੑ͕͍ͭͨɺObjective-Cޓ׵ͷϝ ιουͰͳ͍ͱ͍͚ͳ͍Β͍͠ɻ
  20. ͞Βʹ… • When you define a Swift class that inherits

    from NSObject or any other Objective-C class, the class is automatically compatible with Objective-C. • Ͳ͏΍ΒObjective-CͷΫϥεΛܧঝͨ͠SwiftͷΫ ϥε͸ࣗಈతʹObjective-Cޓ׵ʹͳΔΒ͍͠ɻ • ͖ͬ͞ͷྫͰݺͼग़͍ͯͨ͠characterAtIndex͸ Objective-Cͷϝιου͔ͩΒಈతʹݺͼग़ͤͨɻ
  21. ؾΛऔΓ௚ͯ͠ // Objective-Cޓ׵ͷΫϥεɻ @objc class SomeClass { func someFunc() {

    return } } ! // AnyObjectʹIntܕͷ஋Λ୅ೖɻ var anyObj : AnyObject = 1 ! // ࣮૷͞Ε͍ͯͳ͍ϝιουͷݺͼग़͠... anyObj.someFunc()
  22. -[__NSCFNumber someFunc]: unrecognized selector sent to instance 0x137

  23. unrecognized selector

  24. ͜ͷ੠͸…

  25. idܕͩʂʂʂʂ

  26. Selector΋୳͢ let mySEL: Selector = “someFunc" ! // ஫ҙ: Playground্ͩͱΫϥογϡ͢Δɻ

    ! // ※ίϚϯυϥΠϯ͔ΒͷREPLͩͱେৎ෉ɻ
  27. ิ଍:
 จࣈྻޓ׵ʹ͢Δϓϩτίϧ // Selector͸ҎԼͷϓϩτίϧʹదԠ͍ͯ͠ΔͷͰɺ // จࣈྻ͔ΒॳظԽͰ͖Δɻ protocol StringLiteralConvertible : ExtendedGraphemeClusterLiteralConvertible

    { typealias StringLiteralType class func convertFromStringLiteral(value: StringLiteralType) -> Self } ! protocol ExtendedGraphemeClusterLiteralConvertible { typealias ExtendedGraphemeClusterLiteralType class func convertFromExtendedGraphemeClusterLiteral(value: ExtendedGraphemeClusterLiteralType) -> Self }
  28. Target-Actionύλʔϯ͸݈ࡏ class UIResponder : NSObject { . . . func

    canPerformAction(action: Selector, withSender sender: AnyObject!) -> Bool // Allows an action to be forwarded to another target. By default checks - canPerformAction:withSender: to either return self, or go up the responder chain. func targetForAction(action: Selector, withSender sender: AnyObject!) -> AnyObject! . . . }
  29. ݁࿦ • SwiftͰ΋ಈతͳίʔυ͕͔͚Δɻ • ͱ͸͍͏΋ͷͷɺ݁ہͷͱ͜ΖObjective-CΛ ͔·͍ͯ͠Δ͚ͩɻ • Objective-Cͱͷ࿈ܞΛߟ͑ͳ͍ݶΓɺ
 ੵۃతʹ࢖༻͢Δ΂͖͸ͳͦ͞͏ɻ

  30. ͦ΋ͦ΋ɺϝιουͷಈతݺͼग़͠ΛࣙΊ͔ͨΒ
 ߴ଎Խͨ͠ʹҧ͍ͳ͍…
 (੩తʹ͢Ε͹LLVMʹΑΔ࠷దԽͷϝϦοτ΋ड͚Εͦ͏)

  31. ωλ੾Ε
 Swiftখωλू ୈೋ෦

  32. Special Literals • __FILE__
 ݱࡏͷϑΝΠϧ໊ • __LINE__
 ݱࡏͷߦ਺ • __COLUMN__


    ݱࡏͷྻ਺ • __FUNCTION__
 ࣮ߦதͷؔ਺໊
  33. REPLͰ΍Δͱ __FILE__ // => “<REPL>" ! __FUNCTION__// => "$__lldb_expr"

  34. ΧϦʔԽͱ෦෼ద༻

  35. http://ja.wikipedia.org/wiki/ΧϦʔԽ “ΧϦʔԽ (currying) ͱ͸ɺܭࢉػՊֶ෼ ໺ͷٕ๏ͷҰͭɻෳ਺ͷҾ਺ΛͱΔؔ਺ ΛɺҾ਺͕ʮ΋ͱͷؔ਺ͷ࠷ॳͷҾ਺ʯ Ͱ໭Γ஋͕ʮ΋ͱͷؔ਺ͷ࢒ΓͷҾ਺Λ औΓ݁ՌΛฦؔ͢਺ʯͰ͋ΔΑ͏ͳؔ਺ ʹ͢Δ͜ͱɻ”

  36. HaskellɺMLͷΑ͏ʹσϑΥϧτ ͰΧϦʔԽ͞ΕΔΘ͚Ͱ͸ͳ͍ func addTwoNumbers(a: Int, b: Int) -> Int {

    return a + b } ! var partial = addTwoNumbers(4) ! // ίϯύΠϧΤϥʔ
  37. ͜͏ॻ͚͹෦෼ద༻Ͱ͖Δʂ func addTwoNumbers(a: Int)(b: Int) -> Int { return a

    + b } ! var partial = addTwoNumbers(4) // => (b: Int) -> Int ! var reuslt = partial(b: 5) // => 9
  38. Preprocessor Directive • in Swift you use a global constant

    instead.
 
 ϓϦϓϩηοαͰ͸ͳ͘ɺάϩʔόϧఆ਺Λ࢖͏ɻ • Complex macros are used in C and Objective-C but have no counterpart in Swift.
 
 ؔ਺ܗࣜͷϚΫϩʹ͋ͨΔΑ͏ͳ΋ͷ͸ଘࡏ͠ͳ ͍ɻ
  39. ϝιουͷ໋໊ن໿

  40. ૢ࡞ʴલஔࢺελΠϧ͸
 ౿ऻ͞ΕΔ໛༷ class myCounter { func incrementBy(amout: Int, numberOfTimes: Int)

    { // increment } } ! let counter = myCounter() ! // σϑΥϧτͰ͸ɺୈҰҾ਺ͷ֎෦Ҿ਺໊͸লུ͞ΕΔɻ counter.incrementBy(1, numberOfTimes:1)
  41. ୈೋҾ਺Ҏ߱΋
 Ҿ਺໊Λলུ͍ͨ͠৔߹ class myCounter { // ΞϯμʔείΞͰΦʔόʔϥΠυͯ͠͠·͏ɻ func incrementBy(amout: Int,

    _ numberOfTimes: Int) { // increment } } ! let counter = myCounter() ! counter.incrementBy(1, 1)
  42. Overflowͷѻ͍ • SwiftͷσϑΥϧτͷڍಈͰ͸Φʔόʔϑϩʔ ͨ͠৔߹Τϥʔ͕ൃੜ͢Δɻ • ΦʔόϑϩʔΛڐ༰͢Δ৔߹ɺ
 Ξϯύαϯυ(&)͔Β࢝·Δࢉज़ԋࢉࢠΛ࢖༻ ͢Δඞཁ͕͋Δɻ • &+,

    &-, &*, &/, &% ←͍ͭ͜Β
  43. ݺͼग़͠ݩʹؼͬͯ͜ͳ͍
 ؔ਺ͷએݴ ! @noreturn func tryMe() { exit(EXIT_FAILURE) } !

    // ΋ͪΖΜexitࣗମ΋@noreturn @noreturn func exit(_: CInt)
  44. ·ͱΊ

  45. ܕਪ࿦෇͖ͷ੩తܕ෇͚ݴޠ Swift࠷ߴʂʂʂ(৸ฦΔ)

  46. ͋Γ͕ͱ͏͍͟͝·ͨ͠