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
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
330
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
930
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
cmp.Or に感動した
otakakot
3
180
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
880
Arm移行タイムアタック
qnighy
0
330
受け取る人から提供する人になるということ
little_rubyist
0
230
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
330
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Producing Creativity
orderedlist
PRO
341
39k
For a Future-Friendly Web
brad_frost
175
9.4k
How GitHub (no longer) Works
holman
310
140k
We Have a Design System, Now What?
morganepeng
50
7.2k
The Invisible Side of Design
smashingmag
298
50k
Ruby is Unlike a Banana
tanoku
97
11k
Site-Speed That Sticks
csswizardry
0
26
It's Worth the Effort
3n
183
27k
Adopting Sorbet at Scale
ufuk
73
9.1k
A better future with KSS
kneath
238
17k
Side Projects
sachag
452
42k
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