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

iOSアプリ開発者から見たKotlin

 iOSアプリ開発者から見たKotlin

2017/06/29
第6回Kotlin勉強会 @ Sansan
Koishi

Koichiro Oishi

June 29, 2017
Tweet

More Decks by Koichiro Oishi

Other Decks in Programming

Transcript

  1. ΞδΣϯμ • جຊจ๏(ͬ͘͟Γ) • Null҆શ • ؔ਺, ΦϒδΣΫτ(γϯάϧτϯ), ίϯύχΦϯΦϒδΣΫτ(static) •

    σʔλΫϥε • Extension(֦ுؔ਺ɺ֦ுϓϩύςΟ) Koichiro Oishi, 2017/6/29 11
  2. جຊจ๏ - ม਺ͷએݴ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
  3. جຊจ๏ - ม਺ͷએݴ Kotlin val num = 1 var num2

    = 2 Swift let num = 1 var num2 = 2 ܕͷলུ(ܕਪ࿦) Koichiro Oishi, 2017/6/29 14
  4. جຊจ๏ - 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
  5. جຊจ๏ - whenࣜ Swift let value = 1 let str

    = { switch value { case 1: "one" case 2: "two" default: "other" } } Koichiro Oishi, 2017/6/29 16
  6. 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
  7. Null҆શ - Nullڐ༰ܕ Kotlin var b: String? = "abc" b

    = null // => OK Swift var b: String? = "abc" b = nil // => OK Koichiro Oishi, 2017/6/29 18
  8. 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
  9. 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
  10. 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
  11. Null҆શ - ΤϧϏεԋࢉࢠ Kotlin val l: Int = b?.length ?:

    -1 Swift let l: Int = b?.characters.count ?? -1 Koichiro Oishi, 2017/6/29 22
  12. Null҆શ - !!ԋࢉࢠ Kotlin val l: Int = b!!.length b͕Null(nil)ͩͬͨΒ

    KotlinͰ͸NullPointerException͕ൃੜ Koichiro Oishi, 2017/6/29 23
  13. Null҆શ - !!ԋࢉࢠ Swift let l: Int = b!.characters.count ڧ੍Ξϯϥοϓ

    b͕Null(nil)ͩͬͨΒ SwiftͰ͸Ϋϥογϡ͠·͢ Koichiro Oishi, 2017/6/29 24
  14. Null҆શ - Swiftͷ৔߹ if let count = b?.characters.count { print(count)

    //ඇoptionalܕ } if letͷϒϩοΫ಺Ͱ͸Nullෆڐ༰ܕ(ඇoptionalܕ) ͱͯ͠࢖͑Δ Koichiro Oishi, 2017/6/29 25
  15. Null҆શ - Swiftͷ৔߹ guard let count = b?.characters.count else {

    return } print(count) //ඇoptionalܕ guardจҎ߱Ͱ͸Nullෆڐ༰ܕ(ඇoptionalܕ)ͱ͠ ͯ࢖͑Δ Koichiro Oishi, 2017/6/29 26
  16. ؔ਺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
  17. ΦϒδΣΫτ(γϯάϧτϯ)5 Kotlin ఆٛ object DataProviderManager { fun registerDataProvider(provider: DataProvider) {

    // ... } } ݺͼग़͠ DataProviderManager.registerDataProvider(...) 5 Kotlinॿ૸ಡຊ p.47 Koichiro Oishi, 2017/6/29 28
  18. ΦϒδΣΫτ(γϯάϧτϯ) Swift ఆٛ class DataProviderManager { static let sharedInstance =

    DataProviderManager() private init() { ... } func registerDataProvider(provider: DataProvider) { } } ݺͼग़͠ DataProviderManager.sharedInstance.registerDataProvider(...) Koichiro Oishi, 2017/6/29 29
  19. ίϯύχΦϯΦϒδΣΫτ(static) Kotlin ఆٛ class MyClass { companion object Factory {

    fun create(): MyClass = MyClass() } } ݺͼग़͠ val instance = MyClass.create() Koichiro Oishi, 2017/6/29 30
  20. ίϯύχΦϯΦϒδΣΫτ(static) Swift ఆٛ class MyClass { static func create() ->

    MyClass { return MyClass() } } ݺͼग़͠ let test = MyClass.create() Koichiro Oishi, 2017/6/29 31
  21. σʔλΫϥε6 Kotlin data class Person(val id: Long, var name: String)

    Kotlinʹ͸Pair΍TripleͳͲ΋͋Δ͕ɺ ม਺໊͸͚ͭΒΕͳ͍ͬΆ͍ Swift typealias Person = (id: Int, name: String) σʔλΫϥε = ׬શʹSwiftͷλϓϧͰ΋ͳ͘ SwiftͷStructͬΆ͞΋͋Δʁ 6 Kotlinॿ૸ಡຊ p.52 Koichiro Oishi, 2017/6/29 32
  22. 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
  23. 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
  24. 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