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
iOSアプリ開発者から見たKotlin
Search
Koichiro Oishi
June 29, 2017
Programming
2
690
iOSアプリ開発者から見たKotlin
2017/06/29
第6回Kotlin勉強会 @ Sansan
Koishi
Koichiro Oishi
June 29, 2017
Tweet
Share
More Decks by Koichiro Oishi
See All by Koichiro Oishi
アプリのユニットテストについて初歩から考えてみる/ABC UnitTest
koishi
3
800
Kotlin/Swift 有理数クラスのすすめ
koishi
0
1.2k
5分でわかるPassword_Autofill_for_Apps
koishi
0
1k
Other Decks in Programming
See All in Programming
14 Years of iOS: Lessons and Key Points
seyfoyun
1
770
선언형 UI에서의 상태관리
l2hyunwoo
0
140
The rollercoaster of releasing an Android, iOS, and macOS app with Kotlin Multiplatform | droidcon Italy
prof18
0
150
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
180
Zoneless Testing
rainerhahnekamp
0
120
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
290
プロダクトの品質に コミットする / Commit to Product Quality
pekepek
2
770
.NET 9アプリをCGIとして レンタルサーバーで動かす
mayuki
1
770
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
330
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
440
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
260
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
KATA
mclloyd
29
14k
Adopting Sorbet at Scale
ufuk
73
9.1k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
Typedesign – Prime Four
hannesfritz
40
2.4k
Six Lessons from altMBA
skipperchong
27
3.5k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
440
The Cult of Friendly URLs
andyhume
78
6.1k
Optimising Largest Contentful Paint
csswizardry
33
3k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
It's Worth the Effort
3n
183
28k
Transcript
iOSΞϓϦ։ൃऀ͔Βݟͨ Kotlin 2017/06/29 ୈ6ճKotlinษڧձ ˏ Sansan Koishi Koichiro Oishi, 2017/6/29
1
ࣗݾհ େੴ ߂Ұ γεςϜɾΞφϥΠζגࣜձࣾ iOSΞϓϦ։ൃऀ ΞΧϯτؔ࿈ * @mego_ * koishi
Koichiro Oishi, 2017/6/29 2
ίϯςΩετ iOSΞϓϦΤϯδχΞͰ͢ɻ Objective-CͱSwiftࣝ͋Δ(͋Δఔ) JavaͳΜͱͳͬͯ͘Δɻ Kotlin໊લͱͳΜͱͳ͘Γ্͕ΓΛɻ Koichiro Oishi, 2017/6/29 3
Kotlin͔Θ͍͍ ͱ͍͏͜ͱΛઌͷษڧձͰΓ·ͨ͠ Koichiro Oishi, 2017/6/29 4
ͦͯ͠ SwiftͱࣅͯΔ ͱ͍͏͜ͱᷚͰΓ·ͨ͠ Koichiro Oishi, 2017/6/29 5
ຊͦΜͳϨϕϧͰݟͨ KotlinͱSwiftͷҧ͍Λ ൃද͖ͤͯ͞·͢ɻ Koichiro Oishi, 2017/6/29 6
ࢀߟจݙ Swift ࣮ફೖ ஶऀͷ1ਓɺੴ͞Μ͔ΒαΠ ϯΒ͍·ͨ͠ɻ Koichiro Oishi, 2017/6/29 7
ࢀߟจݙ Kotlin ελʔτϒοΫ ௨শʮ͜ຊʯ ஶऀͷͨΖ͏͞Μ͔ΒαΠϯ Β͍·ͨ͠ɻ Koichiro Oishi, 2017/6/29 8
ࢀߟจݙ ೖ·Ͱͷ Kotlin ॿಡຊ ࢲʹͱͬͯྑͦ͞͏Ͱ͢ ͪ͜ΒΛϕʔεʹSwiftͱൺֱ ͍͖ͯ͠·͢ Koichiro Oishi, 2017/6/29
9
ຊͰ͢ɻ Koichiro Oishi, 2017/6/29 10
ΞδΣϯμ • جຊจ๏(ͬ͘͟Γ) • Null҆શ • ؔ, ΦϒδΣΫτ(γϯάϧτϯ), ίϯύχΦϯΦϒδΣΫτ(static) •
σʔλΫϥε • Extension(֦ுؔɺ֦ுϓϩύςΟ) Koichiro Oishi, 2017/6/29 11
͜ͷ͋ͨΓΛKotlinͱSwift Ͱൺֱͯ͠Έ·͢ Koichiro Oishi, 2017/6/29 12
جຊจ๏ - มͷએݴ1 Kotlin val num : Int = 1
var num2 : Int = 2 Swift let num: Int = 1 var num2: Int = 2 valͱletఆ 1 Kotlinॿಡຊ p.28 Koichiro Oishi, 2017/6/29 13
جຊจ๏ - มͷએݴ Kotlin val num = 1 var num2
= 2 Swift let num = 1 var num2 = 2 ܕͷলུ(ܕਪ) Koichiro Oishi, 2017/6/29 14
جຊจ๏ - whenࣜ2 Kotlin val value = 1 val str
= when(value) { 1-> "one" 2-> "two" else -> "other" } 2 Kotlinॿಡຊ p.30 Koichiro Oishi, 2017/6/29 15
جຊจ๏ - whenࣜ Swift let value = 1 let str
= { switch value { case 1: "one" case 2: "two" default: "other" } } Koichiro Oishi, 2017/6/29 16
Null҆શ - Nullෆڐ༰ܕ3 Kotlin var a: String = "abc" a
= null // => ίϯύΠϧΤϥʔ Swift var a: String = "abc" a = nil // => ίϯύΠϧΤϥʔ 3 Kotlinॿಡຊ p.33 Koichiro Oishi, 2017/6/29 17
Null҆શ - Nullڐ༰ܕ Kotlin var b: String? = "abc" b
= null // => OK Swift var b: String? = "abc" b = nil // => OK Koichiro Oishi, 2017/6/29 18
Null҆શ - Null νΣοΫͱ҆શͳݺͼग़͠ Kotlin Null ෆڐ༰ܕͷࢀর var a: String
= "abc" val l : Int = a.length // => OK Null ڐ༰ܕͷࢀর var b: String? = "abc" val l : Int = b.length // => ίϯύΠϧΤϥʔ Koichiro Oishi, 2017/6/29 19
Null҆શ - Null ڐ༰ܕͷϝιουݺͼग़͠ Kotlin Null ෆڐ༰ܕͷࢀর var b: String?
= "abc" val l : Int = if(b != null) b.length else -1 Null ڐ༰ܕͷࢀর val l : Int? = b?.length Koichiro Oishi, 2017/6/29 20
Null҆શ - Null ڐ༰ܕͷϝιουݺͼग़͠ Swift Null ෆڐ༰ܕͷࢀর var b: String?
= "abc" let l: Int = b?.characters.count ?? -1 Null ڐ༰ܕͷࢀর let l: Int? = b?.characters.count Koichiro Oishi, 2017/6/29 21
Null҆શ - ΤϧϏεԋࢉࢠ Kotlin val l: Int = b?.length ?:
-1 Swift let l: Int = b?.characters.count ?? -1 Koichiro Oishi, 2017/6/29 22
Null҆શ - !!ԋࢉࢠ Kotlin val l: Int = b!!.length b͕Null(nil)ͩͬͨΒ
KotlinͰNullPointerException͕ൃੜ Koichiro Oishi, 2017/6/29 23
Null҆શ - !!ԋࢉࢠ Swift let l: Int = b!.characters.count ڧ੍Ξϯϥοϓ
b͕Null(nil)ͩͬͨΒ SwiftͰΫϥογϡ͠·͢ Koichiro Oishi, 2017/6/29 24
Null҆શ - Swiftͷ߹ if let count = b?.characters.count { print(count)
//ඇoptionalܕ } if letͷϒϩοΫͰNullෆڐ༰ܕ(ඇoptionalܕ) ͱͯ͑͠Δ Koichiro Oishi, 2017/6/29 25
Null҆શ - Swiftͷ߹ guard let count = b?.characters.count else {
return } print(count) //ඇoptionalܕ guardจҎ߱ͰNullෆڐ༰ܕ(ඇoptionalܕ)ͱ͠ ͯ͑Δ Koichiro Oishi, 2017/6/29 26
ؔ4 Kotlin fun add(x: Int, y: Int): Int { return
x + y } Swift func add(x: Int, y: Int) -> Int { return x + y } 4 Kotlinॿಡຊ p.36 Koichiro Oishi, 2017/6/29 27
ΦϒδΣΫτ(γϯάϧτϯ)5 Kotlin ఆٛ object DataProviderManager { fun registerDataProvider(provider: DataProvider) {
// ... } } ݺͼग़͠ DataProviderManager.registerDataProvider(...) 5 Kotlinॿಡຊ p.47 Koichiro Oishi, 2017/6/29 28
ΦϒδΣΫτ(γϯάϧτϯ) Swift ఆٛ class DataProviderManager { static let sharedInstance =
DataProviderManager() private init() { ... } func registerDataProvider(provider: DataProvider) { } } ݺͼग़͠ DataProviderManager.sharedInstance.registerDataProvider(...) Koichiro Oishi, 2017/6/29 29
ίϯύχΦϯΦϒδΣΫτ(static) Kotlin ఆٛ class MyClass { companion object Factory {
fun create(): MyClass = MyClass() } } ݺͼग़͠ val instance = MyClass.create() Koichiro Oishi, 2017/6/29 30
ίϯύχΦϯΦϒδΣΫτ(static) Swift ఆٛ class MyClass { static func create() ->
MyClass { return MyClass() } } ݺͼग़͠ let test = MyClass.create() Koichiro Oishi, 2017/6/29 31
σʔλΫϥε6 Kotlin data class Person(val id: Long, var name: String)
KotlinʹPairTripleͳͲ͋Δ͕ɺ ม໊͚ͭΒΕͳ͍ͬΆ͍ Swift typealias Person = (id: Int, name: String) σʔλΫϥε = શʹSwiftͷλϓϧͰͳ͘ SwiftͷStructͬΆ͋͞Δʁ 6 Kotlinॿಡຊ p.52 Koichiro Oishi, 2017/6/29 32
Extension - ֦ுؔ(Extension Function)7 Kotlin fun String.appendBeer() : String =
"${this}beer!" println("I like".appendBeer()) // => I like beer! Swift extension String { func appendBeer() -> String { return "\(self)beer!" } } print("I like".appendBeer()) 7 Kotlinॿಡຊ p.55 Koichiro Oishi, 2017/6/29 33
Extension - ֦ுϓϩύςΟ(Extension Properties) Kotlin ఆٛ val <T> List<T>.lastIndex: Int
get() = size - 1 ݺͼग़͠ val arr = listOf(1,2,3) println(arr.lastIndex) //=> 2 Koichiro Oishi, 2017/6/29 34
Extension - ֦ுϓϩύςΟ(Extension Properties) Swift ఆٛ extension Array { var
lastIndex: Int { return self.count - 1 } } ݺͼग़͠ let arr = [1,2,3] print(arr.lastIndex) //=> 2 Koichiro Oishi, 2017/6/29 35
ࠓճ͜͜·ͰͰ͕͢ɺ ͜Ε͔ΒKotlin͍͖ͬͯ·͢ ߴ֊ؔɾϥϜμɾΞΫηε੍ݶ͋ͨΓ औΓ্͔͛ͨͬͨͷͰ͕͢ϘϦϡʔϜతʹ… Koichiro Oishi, 2017/6/29 36
SwiftΛ͋·Γ͝ଘͳ͍ํɺ ͍͔͕Ͱͨ͠Ͱ͠ΐ͏͔ Koichiro Oishi, 2017/6/29 37
Swift ͔Θ͍͍ͷͰ? ͱݴͬͯΈ͔ͨͬͨͷͰ͢ Koichiro Oishi, 2017/6/29 38
࠷ޙʹ Kotlinͷ͕͜͜ྑ͍ ͱ͔ڭ͍͑ͯͩ͘͞ʂ Koichiro Oishi, 2017/6/29 39
ͪͳΈʹ ͜ͷαΠτͰKotlinͱSwiftͷҧ ͍͕·ͱ·ͬͯ·͢Ͷɻ Swift is like Kotlin http://nilhcem.com/swift-is- like-kotlin/ Koichiro
Oishi, 2017/6/29 40
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ Koichiro Oishi, 2017/6/29 41