struct ViewA: View { @State private var isShowingViewB = false var body: some View { VStack { Button("Show View B") { isShowingViewB = true } NavigationLink( isActive: $isShowingViewB, destination: { ViewB() }, label: { EmptyView() } ) } } }
Dictionaryのキー存在しない時デフォルトバリューを提供する
let dic = ["dog": 1, "cat": 2] print(dic["chicken", default: 0]) // outputは0
SwiftのDataをKotlinのByteArrayに変換
extension Data { func toKotlinByteArray() -> KotlinByteArray { let byteArray = [UInt8](self) let kotlinByteArray = KotlinByteArray(size: Int32(byteArray.count)) for (i, byte) in byteArray.enumerated() { kotlinByteArray.set(index: Int32(i), value: Int8(bitPattern: byte)) } return kotlinByteArray } }
Terminalの小技
実行を中止:Ctrl + C
入力行を削除:Ctrl + U
入力行の最後の単語を削除:Ctrl + W
Array(repeating, count)の罠
Array(repeating, count)はクラスを作成の時には使えない。
例えばArray(repeating: UIView(frame: .zero), count: 5)
を書いた時実に作ったViewは一つだけ。五つのポインターは同じViewに指しているだけ。
正しいやり方は(0...4).map { _ in UIView(frame: .zero) }
だ。