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

第六回カジュアルSwift勉強会@青葉台

ZuQ9Nn
April 02, 2016

 第六回カジュアルSwift勉強会@青葉台

ZuQ9Nn

April 02, 2016
Tweet

More Decks by ZuQ9Nn

Other Decks in Programming

Transcript

  1. Ϋϩʔδϟʹ͍ͭͯ ୈ6ճɹΧδϡΞϧSwiftษڧձ@੨༿୆ 2016/04/02(౔) ZuQ9Nn

  2. ࣗݾ঺հ ZuQ9Nn(͖ͣΎʙΜ) ීஈ͸Objective-CͰiOSΞϓϦ։ൃ(ձࣾһͰ͢) ࣮຿Ͱ͸SwiftΛ࢖ͬͨ͜ͱ͸ແ͍Αɻ ݸਓͰΞϓϦ΋ग़ͯ͠·͢ɻ(࠷ۙ͸ఀ଺) ؾʹͳΔਓ͸AppStoreͰZuQ9NnΛݕࡧʂ μ΢ϯϩʔυͯ͠ϨϏϡʔΛ ೖΕͯ΋Β͑ΔͱྭΈʹͳΓ·͢

  3. ͸͡Ίʹ ࣗ෼͕Ϋϩʔδϟ͕ۤखͳͷͰ ൃද͢Δ͜ͱͰษڧ͠Α͏͔ͳ͊ͱɻɻ ͋·Γɺਂ͍಺༰͸͋Γ·ͤΜɻ ͢Ͱʹ஌͍ͬͯΔํɺৄ͍͠ํʹ͸෺଍Γͳ͍͔΋ɻ ϚαΧϦ͸౤͍͍͛ͯͨͩͯ΋ ൃදͦͷ΋ʹ׳Εͯ·ͤΜͷͰ ରԠ͔͠ͶΔ͔ͱ͓΋͍·͢ͷͰ͓खॊΒ͔ʹɻ ๻ͱಉ͡Α͏ʹΫϩʔδϟ͕ۤखͳਓ͕গ͠Ͱ΋ ॿ͚ʹͳΕ͹ྑ͍͔ͳ͊ɻ

  4. What is Ϋϩʔδϟʁ Ϋϩʔδϟͱ͸ ແ໊ؔ਺ʂ ͭ·Γ໊લͷͳ͍ؔ਺Ͱ͢ɻ ͱ͍ͬͯ΋ɺ͍͖ͳΓΫϩʔδϟΛॻ͖࢝ΊΔͷ͸೉͍͠ͱ ݸਓతʹ͸ײ͍ͯ͡·͢ɻ ͦ͜Ͱɺ·ͣ͸ɺී௨ͷ໊લͷ͋Δؔ਺ΛΫϩʔδϟʹ͍ͯ͘͠ εςοϓΛ;Ή͜ͱͰΫϩʔδϟΛॻ͘͜ͱʹ

    ׳Ε͍͖ͯ·͠ΐ͏ʂʂ ྫ͸଍͠ࢉΛ͢Δؔ਺ func add(x: Int, y: Int) -> Int { return x + y }
  5. εςοϓ̍ ໊લͷແ͍ؔ਺ͬͯݴ͏͘Β͍ͳͷͰɺؔ਺໊Λ࡟আʂ func add(x: Int, y: Int) -> Int {

    return x + y } func add(x: Int, y: Int) -> Int { return x + y } (x: Int, y: Int) -> Int { return x + y }
  6. εςοϓ2 ؔ਺ͷ໊લΛ࡟আ͢Δͱจষͷ࢝·Γ͕Θ͔Βͳ͘ͳΔɻ ifจswitchจΛݟͯΘ͔ΔΑ͏ʹSwiftͰͷจষ͸ தׅހɹ{} ͰғΉͷͰதׅހͷ࢝·ΓΛҠಈ (x: Int, y: Int) ->

    Int { return x + y } { (x: Int, y: Int) -> Int return x + y } { (x: Int, y: Int) -> Int { return x + y }
  7. εςοϓ3 தׅހͷ࢝·ΓΛҠಈ͢Δͱࠓ౓͸ɺ ؔ਺ͷએݴͷ෦෼ͱ࣮૷ͷ෦෼ͷڥ໨͕Θ͔Βͳ͘ͳΔͷͰɺ ͦͷڥ໨ʹ in Λ͚ͭΔ { (x: Int, y:

    Int) -> Int in return x + y } ͜ΕͰΫϩʔδϟͷ׬੒ɹ*\(^o^)/* { (x: Int, y: Int) -> Int ɹ return x + y } ˡؔ਺ͷఆٛ ˡؔ਺ͷ࣮૷
  8. ؔ਺ΦϒδΣΫτʹ͍ͭͯ ໊લͷ͋Δී௨ͷؔ਺ͳΒɺ ໊લͰݺͼग़ͤ͹͍͍͚Ͳ ͦ΋ͦͷ໊લͷແ͍ؔ਺ͬͯͲ͏΍ͬͨΒݺͼग़͢ͷʁ ͦ͜Ͱར༻͢Δͷ͕ؔ਺ΦϒδΣΫτ

  9. ؔ਺ΦϒδΣΫτʹ͍ͭͯ ɹɹɹ ɹɹ= { (x: Int, y: Int) -> Int

    in return x + y } ؔ਺ΦϒδΣΫτͷར༻ ̍. ม਺Λ༻ҙ͢Δ ̎. ม਺ʹΫϩʔδϟΛ୅ೖ͢Δ ड͚ͨม਺͕ؔ਺ͦͷ΋ͷʹͳΓɺ Ҿ਺Λࢦఆ͢Δ͜ͱͰΫϩʔδϟͷ ࣮૷ΛΑͼͩ͢͜ͱ͕Ͱ͖Δ result(5, 10) let result / / ஋͸15
  10. ؔ਺ΦϒδΣΫτʹ͍ͭͯ ؔ਺ΛΦϒδΣΫτɺҰͭͷ஋ͱͯ͠ѻ͑Δͬͯ͜ͱ͸ɺ String΍IntͷΑ͏ͳܕΈ͍ͨʹ࢖͑Δͱ͍͏͜ͱ class Sample1 { let name: String =

    “Sample1” let sampleFunc = { (x: Int, y: Int) -> Int in return x + y } } let sample1: Sample1 = Sample1() sample1.name sample1.sampleFunc(10, 5)
  11. ؔ਺ΦϒδΣΫτʹ͍ͭͯ ܕਪ࿦Λলུ͠ͳ͍ܗͰॻ͘ͱ ؔ਺ͷఆٛ෦෼͕ܕʹͳΔɻ class Sample2 { let name: String =

    “Sample2” let sampleFunc: (x: Int, y: Int) -> Int = { (x: Int, y: Int) -> Int in return x + y } } ˡؔ਺ͷఆٛ let sample2: Sample2 = Sample2() sample2.name sample2.sampleFunc(x: 10, y: 5)
  12. ؔ਺ΦϒδΣΫτʹ͍ͭͯ Ϋϩʔδϟ͚ͩͰͳ͘ɺී௨ͷ໊લͷ͋Δؔ਺΋ؔ਺ ΦϒδΣΫτΛར༻Ͱ͖ΔͷͰצҧ͍͠ͳ͍ͰͶɻ

  13. ಥવͷ࢓༷มߋʂʂ ࠓͷྫͰ͸଍͠ࢉͱ͍͏࣮૷΋ؚΜͩΫϩʔδϟΛѻͬ ͍ͯΔ͚ͲɺҾ͖ࢉ΋͔͚ࢉͰ͖ΔΑ͏ʹ͍ͨ͠ʂʂ ͜ͷ࢓༷௥Ճ͕͋ͬͨ৔߹Λߟ͑ͯΈΑ͏

  14. ͜Μͳίʔυ΋ॻ͚Δ͚Ͳ class Sample3 { func getSampleFunc(type: Int) -> ((x: Int,

    y: Int) -> Int) { var result = { (x: Int, y: Int) -> Int in return x + y } switch type { case 0: result = { (x: Int, y: Int) -> Int in return x - y } case 1: result = { (x: Int, y: Int) -> Int in return x * y } default: break } return result } }
  15. ࣮૷ͷ͋Δ৔߹ ࣮૷Λ΋ͬͨΫϩʔδϟͩͱ ࢓༷มߋɺ௥Ճ͕ى͖ͨ৔߹ɺ ͳΜͲ΋࣮૷෦෼ίʔυͷॻ͖׵͑Δඞཁ͕ग़ͯ͘Δ

  16. ࣮͸ ࣮૷ͷແ͍ɺؔ਺ͷఆٛͷ෦෼ͷΈͷΫϩʔδϟ ؔ਺ΦϒδΣΫτͱͯ͠࢖͏͜ͱ͕Մೳʂʂ ࣮૷ͷແ͍Ϋϩʔδϟ͸ݺͼग़͠ݩͰ࣮૷Λߦ͏ɻ

  17. ࣮૷ͷແ͍Ϋϩʔδϟ class Sample4 { func getSampleFunc( paramX: Int, paramY: Int,

    result: ((x: Int, y: Int) -> Void)) { result(x: paramX, y: paramY) } } let sample4: Sample4 = Sample4() sample4.getSampleFunc(100, paramY: 10, result: { (x: Int, y: Int) -> Void in print(x + y) print(x - y) print(x * y) }) ˡ଍͠ࢉͰ΋Ҿ͖ࢉͰ΋ ࢥ͍ͷ··
  18. Ϋϩʔδϟͷར఺ Ϋϩʔδϟ͸࣮૷ͷແ͍ؔ਺ͷఆٛͷ෦෼͚ͩΛѻ͏͜ͱɺ ݺͼग़͠ݩͰࣗ༝ʹ࣮૷Ͱ͖ΔࣄʹΑͬͯɺ มߋʹڧ͍ॊೈͳίʔυ͕ॻ͚ΔΑ͏ʹͳΓ·͢ʂʂ

  19. ม਺ͷΩϟϓνϟͱϝϞϦͷϦʔΫ ͜ΜͳʹศརͳΫϩʔδϟ͕ͩɺ஫ҙ఺͕͋Γ·͢ɻ ͦΕ͕ม਺ͷΩϟϓνϟͷ໰୊Ͱ͢ɻ ม਺ͷΩϟϓνϟͱ͸ Ϋϩʔδϟͷ֎Ͱએݴ͞Εͨม਺Λ Ϋϩʔδϟ಺Ͱར༻͢Δͱ ม਺ͷΩϟϓνϟ͕ߦΘΕΔɻ Ωϟϓνϟ͞Εͨม਺͸Ϋϩʔδϟ͕ऴྃͯ͠΋ɺ ϝϞϦ͕ղ์͞Εͳ͍ɻ

  20. ม਺ͷΩϟϓνϟͱϝϞϦͷϦʔΫ class Sample5 { var total : Int = 0

    lazy var result: (x: Int, y: Int) -> Void = { (x: Int, y: Int) -> Void in self.total = x + y } } let sample5: Sample5 = Sample5() sample5.result(x: 100, y: 10) sample5.total // →஋͸110  ΋͠Ϋϩʔδϟऴྃޙ ม਺͕ഁغ͞ΕͨΒ total͸0ʹͳΔ Πϯελϯεม਺Ͱ એݴ͍ͯ͠Δtotal͕ Ωϟϓνϟ͞ΕΔ
  21. ม਺ͷΩϟϓνϟͱϝϞϦͷϦʔΫ class Sample5 { var total : Int = 0

    lazy var result: (x: Int, y: Int) -> Void = { (x: Int, y: Int) -> Void in self.total = x + y } } ม਺ͷΩϟϓνϟ͕ى͜ΔͱࢀরΧ΢ϯτ͕Ճࢉ͞Ε ΦϒδΣΫτ͕ഁغ͞Εͳ͘ͳΔ৔߹͕͋Δ var sample5: Sample5? = Sample5() sample5?.result(x: 100, y: 10) sample5 = nil ˡࢀরΧ΢ϯτʴ̍ ˡࢀরΧ΢ϯτʴ̍ ˡࢀরΧ΢ϯτʔ̍
  22. ม਺ͷΩϟϓνϟͱϝϞϦͷϦʔΫ ϝϞϦϦʔΫΛ๷͙ʹ͸Ϋϩʔδϟͷ࢝·Γͷ௚ޙʹɹ [unowned self] Λ͚ͭΔ class Sample6 { var total:

    Int = 0 lazy var result: (x: Int, y: Int) -> Void = { (x: Int, y: Int) -> Void in self.total = x + y } } class Sample6 { var total: Int = 0 lazy var result: (x: Int, y: Int) -> Void = {[unowned self] (x: Int, y: Int) -> Void in self.total = x + y } }
  23. ม਺ͷΩϟϓνϟͱϝϞϦͷϦʔΫ ͱ͸ݴͬͯ΋ຊ౰ʹղ์͞Ε͍ͯΔ͔৺഑ʁʁ deinitΛهड़ͯ͠ղ์Λ֬ೝͯ͠ΈΑ͏ class Sample7 { var total: Int =

    0 lazy var result: (x: Int, y: Int) -> Void = {[unowned self] (x: Int, y: Int) -> Void in self.total = x + y } deinit { print(“Sample7 class deinit call”) } } var sample7: Sample7? = Sample7() sample7?.result(x: 100, y: 10) sample7?.total sample7 = nil
  24. ม਺ͷΩϟϓνϟͱϝϞϦͷϦʔΫ ม਺ͷΩϟϓνϟ͕ى͜ΔͱඞͣϝϞϦϦʔΫ͕ൃੜ͢ ΔΘ͚Ͱ͸ແ͍ɻSwift͕ࣗಈతʹϝϞϦΛղ์͢Δ৔߹ ΋͋ΔͷͰצҧ͍͠ͳ͍ͰͶɻ

  25. XcodeͰͷϝϞϦϦʔΫνΣοΫ ϝϞϦϦʔΫ͸ίʔυΛಡΜͰ΋൑அͣ͠Β͍͠ɺ ΋͏͢Ͱʹ͔ͳΓͷྔͷίʔυΛॻ͍ͯ͠·͍ͬͯΔɻ ͦ͜ʹdeinitΛ௥Ճͯ֬͠ೝ͍ͯ͘͠ͷ͸େมͬͯਓͷͨΊʹ XcodeͰͷϝϞϦϦʔΫͷνΣοΫͷ࢓ํΛղઆ͠·͢ɻ

  26. XcodeͰͷϝϞϦϦʔΫνΣοΫ 1. ϝχϡʔ͔ΒProfileΛબ୒

  27. XcodeͰͷϝϞϦϦʔΫνΣοΫ 2. Instruments͕ىಈ͠chose a profiling template forͷϝ χϡʔ͕දࣔ͞ΕΔͷͰLeaksΛબ୒

  28. XcodeͰͷϝϞϦϦʔΫνΣοΫ 3. Leaks͕ىಈͨ͠Βɺ΋͏Ұ౓XcodeΛProfileͷ··࣮ߦ

  29. XcodeͰͷϝϞϦϦʔΫνΣοΫ 4.໰୊ແ͍৔߹͸Leaksʹ྘ͷνΣοΫ͕දࣔ͞ΕΔ

  30. XcodeͰͷϝϞϦϦʔΫνΣοΫ 5.ϦʔΫ͍ͯ͠Δ৔߹͸੺͍όπϚʔΫ͕දࣔ͞ΕΔɺ ·ͨࢦఠՕॴΛΫϦοΫ͢Δͱৄࡉ৘ใ͕ݟ͑Δ

  31. ·ͱΊ * Ϋϩʔδϟͱ͸ແ໊ؔ਺ͷ͜ͱ * ͍͖ͳΓΫϩʔδϟΛॻ͘ͷ͕ۤखͳਓ͸·ͣɺ໊લͷ͋Δී௨ͷؔ ਺Λ͔͍ͯΫϩʔδϟʹ͍ͯ͜͠͏ɻ * Ϋϩʔδϟ͸ΦϒδΣΫτɺ஋ͭ·Γɺܕͱͯ͠ར༻Ͱ͖Δ * ࣮૷ͷແ͍ΫϩʔδϟΛར༻͠ݺͼग़͠ݩͰ࣮૷Λهड़͢Δ͜ͱͰ

    มߋʹڧ͍ॊೈͳίʔυ͕ॻ͘ࣄ͕Ͱ͖Δɻ * ม਺ͷΩϟϓνϟʹؾΛ͚ͭͯ॥؀ࢀরͷίʔυΛ࡞Γ͜ΜͰ͠·͏ * ϦʔΫ͕৺഑ͳΒXcodeͰνΣοΫ͓͖ͯ͠·͠ΐ͏ * ΫϩʔδϟΛ࢖͍͜ͳͯ͠ॳڃऀ͔ΒதڃऀͷϓϩάϥϚͷୈҰาʹʂ
  32. One More Thing

  33. ࣍ճ༧ࠂʂ ษڧձͰൃද͢Δ͢Δ࠮ٗΛଓ͚ͯ ͳ͔ͳ͔ࢥ͍੾Εͳ͍ͷͰɺ ͜͜Ͱ࣍ճ΋ൃද͢Δ͜ͱΛ༧ࠂ͢Δ͜ͱͰ ࣗ෼Λ௥͍ࠐ΋͏ͱ͓΋͍·͢ɻɻɻ ͱ͍͏ࣄͰɺ࣍΋ൃද͠·͢ɻ (͜ͷษڧ͡Όͳ͍Մೳੑ΋͋Γ·͢)

  34. ษڧձͰͷൃද͸͜Ε͔Βͩʂ ࣍ճͷษڧձൃදΛָ͓͠Έʹɻ

  35. Thank You ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ