暫定。初めてSwiftを書いてみて、ここ必要だなという部分をメモ。
Std Out
Swift
print("")
Kotlin
println("")
Declaring variables
Swift
let number = 42
var message: String = "Hello"
// let -> NG
number = 0
// var -> OK
message = ""
Kotlin
val number = 42
var message: String = "Hello"
// val -> NG
number = 0
// var -> OK
message = ""
Mutable and Immutable List
Swift
let immutableArray: Array<String> = []
var mutableArray: Array<String> = []
// NG
immutableArray.append("")
// OK
mutableArray.append("")
Kotlin
val immutableList: List<String> = listOf()
val mutableList: MutableList<String> = mutableListOf()
// NG
immutableList.add("")
// OK
mutableList.add("")
String Template
Swift
let value = "value"
print("value -> \(value)")
Kotlin
val value = "value"
println("value -> $value")
println("value -> ${value}")
Constructor
値をいじってから別のコンストラクタに移譲できるのがswiftの良い所
swift
convenience init() {
init()
}
kotlin
constructor() : super(){
}
Default Constructor
別パッケージからインスタンス化
Swift
class Hoge {
public init(){
}
}
Hoge()
Kotlin
class Hoge {
}
Hoge()
Getter/Setter
Swiftにバッキングフィールドは無い
Kotlin
var hoge: String = "default_value"
get() {
println("called get hoge")
return field
}
set(value) {
println("called set hoge -> $value")
field = value + "processing"
}
Swift
var hoge: String = "default_value" {
didSet {
// 再帰しない
hoge += "processing"
}
willSet {
// newValueにsetされた値が流れてくる
}
// 定義するとRead-Onlyになるので定義できない
// get {
// print("called get hoge")
// }
}
Getter/Setter内の変数名
Swift
var hgoe: String = "" {
didSet {
print(oldValue)
}
willSet {
print(newValue)
}
}
var hgoe: String = "" {
didSet(oldValue_) {
print(oldValue_)
}
willSet(newValue_) {
print(newValue_)
}
}
Kotlin
var hoge: String = "default_value"
set(value_) {
println("called set hoge -> $value_")
field = value_+ "processing"
}
拡張関数、変数
Swift
class Hoge {}
extension Hoge {
func extend() {}
var extendValue: String {
get {
return ""
}
set {
// there is no backing field
}
}
}
Hoge().extendValue
Hoge().extend()
Kotlin
class Hoge {
}
fun Hoge.extend() {
}
var Hoge.extendValue : String
get() = ""
set(value) {
println(value)
// Cannot have a state
// field = value
}
Interface vs Protocol
Swift
protocol IHoge {
var hoge: String { get }
var mutableHoge: String { get set }
func hoge(value: String)
}
class Hoge: IHoge {
let hoge: String = ""
var mutableHoge: String = ""
func hoge(value _: String) {}
}
Kotlin
interface IHoge {
val hoge : String
var mutableHoge: String
fun hoge(value : String)
}
class Hoge {
override val hgoe : String = ""
override var mutableHoge : String = ""
override fun hgoe(value : String) {
}
}
Interface vs Protocol Generics
KotlinではGenericsはクラス同様行えるが、Swiftではassociatedtypeとtypealiasを使用する。
Kotlin
interface IHoge<T> {
fun hoge(value : T)
}
class Hoge : IHoge<String>{
override fun hoge(value: String) {
}
}
Swift
protocol IHoge {
associatedtype T
init(value: T)
}
class Hoge: IHoge {
typealias T = String
required init(value: Hoge.T) {
}
}
Interface vs Protocol Constructor
Swift
protocol IProto {
init(value : String)
}
class Proto : IProto{
required init(value: String) {
}
}
Kotlin
Kotlinではconstructorの実装強制はできない。
名前空間
Swift
モジュール名が名前空間
なので同一モジュール内に同じ名前のクラス名は作成できない。
別のモジュールに同じ名前のクラス名がある場合は以下で指定できる。
ModuleName.ClassName()
Kotlin
以下のコードで指定する
package com.exsample.namespace
ラムダ
->はin
Swift
map { it in
}
Kotlin
map { it ->
}