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. εςοϓ̍ ໊લͷແ͍ؔ਺ͬͯݴ͏͘Β͍ͳͷͰɺؔ਺໊Λ࡟আʂ 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 }
  2. εςοϓ3 தׅހͷ࢝·ΓΛҠಈ͢Δͱࠓ౓͸ɺ ؔ਺ͷએݴͷ෦෼ͱ࣮૷ͷ෦෼ͷڥ໨͕Θ͔Βͳ͘ͳΔͷͰɺ ͦͷڥ໨ʹ in Λ͚ͭΔ { (x: Int, y:

    Int) -> Int in return x + y } ͜ΕͰΫϩʔδϟͷ׬੒ɹ*\(^o^)/* { (x: Int, y: Int) -> Int ɹ return x + y } ˡؔ਺ͷఆٛ ˡؔ਺ͷ࣮૷
  3. ؔ਺ΦϒδΣΫτʹ͍ͭͯ ɹɹɹ ɹɹ= { (x: Int, y: Int) -> Int

    in return x + y } ؔ਺ΦϒδΣΫτͷར༻ ̍. ม਺Λ༻ҙ͢Δ ̎. ม਺ʹΫϩʔδϟΛ୅ೖ͢Δ ड͚ͨม਺͕ؔ਺ͦͷ΋ͷʹͳΓɺ Ҿ਺Λࢦఆ͢Δ͜ͱͰΫϩʔδϟͷ ࣮૷ΛΑͼͩ͢͜ͱ͕Ͱ͖Δ result(5, 10) let result / / ஋͸15
  4. ؔ਺ΦϒδΣΫτʹ͍ͭͯ ؔ਺ΛΦϒδΣΫτɺҰͭͷ஋ͱͯ͠ѻ͑Δͬͯ͜ͱ͸ɺ 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)
  5. ؔ਺ΦϒδΣΫτʹ͍ͭͯ ܕਪ࿦Λলུ͠ͳ͍ܗͰॻ͘ͱ ؔ਺ͷఆٛ෦෼͕ܕʹͳΔɻ 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)
  6. ͜Μͳίʔυ΋ॻ͚Δ͚Ͳ 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 } }
  7. ࣮૷ͷແ͍Ϋϩʔδϟ 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) }) ˡ଍͠ࢉͰ΋Ҿ͖ࢉͰ΋ ࢥ͍ͷ··
  8. ม਺ͷΩϟϓνϟͱϝϞϦͷϦʔΫ 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͕ Ωϟϓνϟ͞ΕΔ
  9. ม਺ͷΩϟϓνϟͱϝϞϦͷϦʔΫ 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 ˡࢀরΧ΢ϯτʴ̍ ˡࢀরΧ΢ϯτʴ̍ ˡࢀরΧ΢ϯτʔ̍
  10. ม਺ͷΩϟϓνϟͱϝϞϦͷϦʔΫ ϝϞϦϦʔΫΛ๷͙ʹ͸Ϋϩʔδϟͷ࢝·Γͷ௚ޙʹɹ [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 } }
  11. ม਺ͷΩϟϓνϟͱϝϞϦͷϦʔΫ ͱ͸ݴͬͯ΋ຊ౰ʹղ์͞Ε͍ͯΔ͔৺഑ʁʁ 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
  12. ·ͱΊ * Ϋϩʔδϟͱ͸ແ໊ؔ਺ͷ͜ͱ * ͍͖ͳΓΫϩʔδϟΛॻ͘ͷ͕ۤखͳਓ͸·ͣɺ໊લͷ͋Δී௨ͷؔ ਺Λ͔͍ͯΫϩʔδϟʹ͍ͯ͜͠͏ɻ * Ϋϩʔδϟ͸ΦϒδΣΫτɺ஋ͭ·Γɺܕͱͯ͠ར༻Ͱ͖Δ * ࣮૷ͷແ͍ΫϩʔδϟΛར༻͠ݺͼग़͠ݩͰ࣮૷Λهड़͢Δ͜ͱͰ

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