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

Swift の struct の stored property は var にしよう

Swift の struct の stored property は var にしよう

potatotips#39 という勉強会で発表したものです。セリフ付きでQiitaにもアップしています。http://qiita.com/omochimetaru/items/7265e440418b38088ccb

omochimetaru

April 13, 2017
Tweet

More Decks by omochimetaru

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ • ID: omochimetaru @ Qoncept, Inc • Swift, Kotlin,

    C++, TypeScript, C#, Ruby, Python • http://qiita.com/omochimetaru • Android։ൃΛड஫͔ͨ͠ΒKotlinΛΨοπϦ... • null҆શΛޡղ͍ͯ͠Δਓୡ΁ͷϝοηʔδ • SwiftͷδΣωϦοΫͳϓϩτίϧͷม਺͸...
  2. ໎͍ͬͯ·ͤΜ͔ʁ struct SVCat { var name: String var age: Int

    } struct SLCat { let name: String let age: Int }
  3. Immutable ͷ֬ೝ class CCat { init(name: String, age: Int) {

    self.name = name self.age = age } let name: String let age: Int } class MutableCCat { init(cat: CCat) { self.name = cat.name self.age = cat.age } var name: String var age: Int var cat: CCat { return CCat(name: name, age: age) } }
  4. Immutable Λײ͡Δ structBreeder.cat.name = "tama" // ↑↓ var cat =

    structBreeder.cat cat.name = "tama" structBreeder.cat = cat
  5. Immutable Λײ͡Δ structBreeder.cat.name = "tama" // ↑↓ var cat =

    structBreeder.cat cat.name = "tama" structBreeder.cat = cat // ↑↓ let cat = MutableCCat(cat: classBreeder.cat) cat.name = "tama" classBreeder.cat = cat.cat
  6. mutableΫϥεΛڞ༗͢Δ͔Β ࠔͬͨ͜ͱʹͳΔ print(classBreeder1.cat.name) // cat1 is mike classBreeder2.cat = classBreeder1.cat

    classBreeder2.cat.name = "tama" print(classBreeder2.cat.name) // cat2 is tama print(classBreeder1.cat.name) // cat1 is tama ← ! • ଞͷڞ༗ऀ͔Βҙਤͤͣมߋ͕༩͑ΒΕΔ
  7. struct͸ڞ༗Ͱ͖ͳ͍͔Βେৎ෉ print(structBreeder1.cat.name) // cat1 is mike structBreeder2.cat = structBreeder1.cat structBreeder2.cat.name

    = "tama" print(structBreeder2.cat.name) // cat2 is tama print(structBreeder1.cat.name) // cat1 is mike ← ! • ίϐʔʹΑ࣮ͬͯମ͕૿͑ΔͨΊৗʹઐ༗͢Δ
  8. ଓ͖ͷ࿩ • ࠙਌ձ • Discord (SlackΈ͍ͨͳ΍ͭ) ͷ iOS αʔόʔͰ •

    ؅ཧऀ: SwiftϨλʔͷ @_mono ͞Μ • ΞΫηε: ʮDiscord iOSʯͰݕࡧ