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

Objective-C と比べて Swift が強力だと感じた4つの点

Objective-C と比べて Swift が強力だと感じた4つの点

yuuxeno

June 20, 2017
Tweet

More Decks by yuuxeno

Other Decks in Programming

Transcript

  1. mutable(Մม) ͷϠό͞ var item = Item(id: 1, name: “Swift”) item

    = Item(id: 1, name: “Objective-C") ⛏ ৭ʑͳॲཧ ⛏
  2. ແବͳঢ়ଶΛ࡞Βͳ͍ let isImmutable = true var text: String if isImmutable

    { text = "immutable" } else { text = "mutable" } let isImmutable = true let text: String = { if isImmutable { return "immutable" } else { return "mutable" } }()
  3. ϠόΠ΍ͭʹ var ҹΛ͚ͭΔ final class ItemViewController: UIViewController { let ɾɾɾ

    let itemDataStore = ItemDataStore() var item: Item? let totalItem = 100 let ɾɾɾ
  4. • Objective-C ʹ͸ immutable Λදݱग़དྷΔ const ͸͕͋ͬͨɺ mutable Λ໌ࣔతʹදݱ ग़དྷΔ΋ͷ͸ͳ͔ͬͨ

    • ϓϩύςΟͷ readwrite ΋লུՄೳͩͬͨ • let -> var ͷॻ͖׵͑͸ίʔυϨϏϡʔͰؾͮ ͖΍͍͢
  5. mutable ͳ΋ͷΛݮΒͤΔ let items = [Item(id: 1, price: 100), Item(id:

    2, price: 200), Item(id: 3, price: 300)] var fetchedItems = [Item]() for item in items { if item.price >= 200 { fetchedItems.append(item) } } let fetchedItems = items.filter { $0.price >= 200 }
  6. ԿΛ͍ͯ͠Δ͔͕Ұ໨Ͱ෼͔Δ let items = [Item(id: 1, price: 100), Item(id: 2,

    price: 200), Item(id: 3, price: 300)] var fetchedItems = [Item]() for item in items { if item.price >= 200 { fetchedItems.append(item) } } let fetchedItems = items.filter { $0.price >= 200 }
  7. Optional • ͋Δ͔΋͠Εͳ͍ɺͳ͍͔΋͠Εͳ͍ͱ͍͏ ࣄʹରͯ͠໌֬ͳܕ͕Ͱ͖ͨ • ৗʹॻ͖खʹ nil Λҙࣝͤ͞ɺߟ͑ͤ͞Δ ( ͜

    ͷม਺ʹ͸࣮૷্ nil ͕དྷΔͷ͔ͳͲ ) • ஋ΛऔΓग़͢৔߹ʹnil Λඞͣҙࣝͤ͞Δ
  8. protocol extension ͳΒ • ΫϥεΑΓ΋ࡉ͔͍ύʔπʹ෼͚ΒΕ࠶ར༻ੑ͕ߴ ͍ • ΫϥεΑΓந৅౓ͷߴ͍ protocol ʹґଘ͢ΔͨΊม

    ߋʹڧ͍ • Өڹൣғ͸ͦͷ protocol extension ͕࣮ݱ͢Δػೳ ʹґଘ͍ͯ͠Δ෦෼ʹݶΒΕΔͨΊϦϑΝΫλϦϯ ά͕༰қ