Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Objective-C と比べて Swift が強力だと感じた4つの点
Search
yuuxeno
June 20, 2017
Programming
0
310
Objective-C と比べて Swift が強力だと感じた4つの点
yuuxeno
June 20, 2017
Tweet
Share
More Decks by yuuxeno
See All by yuuxeno
DIKitで人間がクラス間の依存関係解決するのを終わらせる
yuuxeno
3
4.3k
iOS アプリ開発における Redux という可能性
yuuxeno
1
700
Other Decks in Programming
See All in Programming
Click-free releases & the making of a CLI app
oheyadam
2
120
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
610
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.5k
Figma Dev Modeで変わる!Flutterの開発体験
watanave
0
110
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
4
640
距離関数を極める! / SESSIONS 2024
gam0022
0
280
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
110
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
670
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Teambox: Starting and Learning
jrom
133
8.8k
Fireside Chat
paigeccino
34
3k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Transcript
Objective-C ͱൺͯ Swift ͕ ڧྗͩͱײͨ͡4ͭͷ
Yusuke Minami • @yuuxeno • iOS ΤϯδχΞ
let , var
mutable (Մม) immutable (ෆม) Λ໌֬ʹ۠ผͰ͖Δ
mutable(Մม) ͷϠό͞ var item = Item(id: 1, name: “Swift”) item
= Item(id: 1, name: “Objective-C") ⛏ ৭ʑͳॲཧ ⛏
mutable ͳͷɾɾɾ • Ͳ͜Ͱ͕มΘΔͷ͔ΛৗʹಡΈख͕ҙࣝ͢ Δඞཁ͕͋ΔͷͰՄಡੑ͕͍ • ࠓͲ͏͍͏ঢ়ଶͳͷ͔ΛৗʹಡΈख͕ҙࣝ͢ Δඞཁ͕͋ΔͷͰͭΒ͍
ίʔυΛಡΜͰ͍Δ࣌ؒͷେ෦Λ mutable ͳͷʹඅ͍ͯ͠Δ • ͜ͷঢ়ଶ͕มΘͬͨ࣌ʹॲཧͲ͏มΘΔͷ ͔ ? • ͦΕʹΑͬͯผͷঢ়ଶͲ͏มΘΔͷ͔ ?
• ҎԼϧʔϓ
όά ՄಡੑԼ ֦ுੑԼ
͍͍ͩͨίΠπͷ͍ͤ
શ͕ͯ immutable ͷੈք
࠷ߴ !
ݱ࣮
ΞϓϦঢ়ଶͩΒ͚ɾɾɾ
શ෦ແ͘͢ͷແཧͳͷͰ
ແବͳঢ়ଶΛ࡞Βͳ͍ 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" } }()
ঢ়ଶΛཧ͢Δ •Redux !
ϠόΠͭʹ var ҹΛ͚ͭΔ final class ItemViewController: UIViewController { let ɾɾɾ
let itemDataStore = ItemDataStore() var item: Item? let totalItem = 100 let ɾɾɾ
• Objective-C ʹ immutable Λදݱग़དྷΔ const ͕͋ͬͨɺ mutable Λ໌ࣔతʹදݱ ग़དྷΔͷͳ͔ͬͨ
• ϓϩύςΟͷ readwrite লུՄೳͩͬͨ • let -> var ͷॻ͖͑ίʔυϨϏϡʔͰؾͮ ͖͍͢
ͦͷ ຊʹඞཁͰ͔͢ ? ͦͷ ຊʹ৻ॏʹѻͬͯ·͔͢ ?
var = = औѻҙ
MutableProperty Variable
map, flatMap, filter, reduce
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 }
ԿΛ͍ͯ͠Δ͔͕ҰͰ͔Δ 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 }
ݟ௨͕͘͢͠͝ྑ͘ͳΔ • mutable ͳͷͲ͜Ͱঢ়ଶ͕มΘΔͷ͔ҙ ࣝ͢Δඞཁ͕͋ΔͷͰಡΈʹ͍͘ • ιʔείʔυ͕͋ΔఔͷنʹͳΔͱɺશ ͯͷ෦ʹ͍ͭͯࡉ෦·Ͱ۩ମతʹԿΛͯ͠ ͍Δ͔ΛѲ͢Δͷແཧ ->
งғؾ͚ͩͰ ௫Έ͍ͨ
var, for, if map, flatMap, filter, reduce
Optional
nil Λ୳ཱྀ͢ʹඅ͕࣌ؒ͢େ෯ʹݮͬͨ
ఆ֎ͷ݁Ռ ɾɾɾCSFBLQPJOU /4-PH OJM OJM OJM
ͳ͓ͥલ ( nil ) ͕ͦ͜ʹ͍ΔΜͩ ?
ͳͥͩΖ͏ • nil νΣοΫΛΕΔͱ͙͢ʹఆ֎ͷॲཧ͕ ߦΘΕΔ • nil νΣοΫΕΔͱఆ֎ͷ͕ͲΜͲΜਂ ͍ͱ͜ΖʹྲྀΕ͍ͯ͘ •
ͳ͔ͥ͋Γ͑ͳ͍ͱ͜Ζʹ nil ͕ೖͬͯΔ
Optional • ͋Δ͔͠Εͳ͍ɺͳ͍͔͠Εͳ͍ͱ͍͏ ࣄʹରͯ͠໌֬ͳܕ͕Ͱ͖ͨ • ৗʹॻ͖खʹ nil Λҙࣝͤ͞ɺߟ͑ͤ͞Δ ( ͜
ͷมʹ্࣮ nil ͕དྷΔͷ͔ͳͲ ) • ΛऔΓग़͢߹ʹnil Λඞͣҙࣝͤ͞Δ
ϛεͬͯ nil Λྲྀͯ͠͠·͏ࣄ͕গͳ͘ͳͬͨ
ଘࡏ͢Δͱଘࡏ͢Δ͔͠Εͳ͍ શ͘ผͷ͜ͱͩͬͨ
protocol extension
ɾɾɾܧঝ $MBTT" $MBTT# #BTF$MBTT $MBTT$
ඞཁҎ্ͷͷʹґଘ͢Δ • ܧঝݩΫϥεͷҰ෦ͷػೳΛ͍͍͕ͨͨΊʹɺ༷ʑͳػೳ Λ࣋ͬͨܧঝݩͷΫϥεશͯʹґଘ͢Δ • Ұ෦ͷػೳΛมߋ͠Α͏ͱ͢ΔͱɺͦͷΫϥεͷܧঝઌͷΫ ϥεશͯνΣοΫ͢Δඞཁ͕͋Δ -> গ͠ͷมߋͰӨڹൣ ғ͕େ͖͍ͷͰ؆୯ʹϦϑΝΫλϦϯάͰ͖ͳ͍
• ܧঝݩͷΫϥε͕༷ʑͳׂΛ࣋ͭͨΊݟ௨͕͘͢͠͝ѱ͘ ͳΔ ( ܧঝઌͷΫϥεΛݟͳ͍ͱͲ͏͍͏ҙਤͰ࣮͞Εͨ ͷ͔͕Θ͔Βͳ͍)
ϦϑΝΫλϦϯάͰ͖ͳ͍ͷͰ ޙͲΜͲΜଠ͍ͬͯ͘ͷΈ
ɾɾɾద߹ $MBTT" $MBTT# QSPUPDPMFYUFOTJPO $MBTT$
protocol extension ͳΒ • ΫϥεΑΓࡉ͔͍ύʔπʹ͚ΒΕ࠶ར༻ੑ͕ߴ ͍ • ΫϥεΑΓநͷߴ͍ protocol ʹґଘ͢ΔͨΊม
ߋʹڧ͍ • Өڹൣғͦͷ protocol extension ͕࣮ݱ͢Δػೳ ʹґଘ͍ͯ͠Δ෦ʹݶΒΕΔͨΊϦϑΝΫλϦϯ ά͕༰қ
None
ࢀߟ • SwiftͷOptionalͷϕετϓϥΫςΟε http://qiita.com/ koher/items/8b6156c8263b9b23c43c