Constants - struct vs enum vs extension - #Swift

case-less enum と private init() な struct。


enum Constants {
  // no cases
  static let animationDuration: TimeInterval = 1.5
}

struct Constants {
  static let animationDuration: TimeInterval = 1.5
  private init() { }
}

👉 `static let` in enum vs struct? - Using Swift - Swift Forums hatena-bookmark

enum の多用が目に付く Swift。

extention で、


extension TimeInterval {
  static let animationDuration: TimeInterval = 1.5
}

とも書きたくなるけども。

方針をはっきり強制してもいいのでは、と思います。

新参組は悩みます。

👉 Swift constants: Struct or Enum - Stack Overflow hatena-bookmark
👉 Kotlin で Constants をどう書くべきか。 hatena-bookmark
👉 Android で 定数 (int)で enum を使うことは hatena-bookmark


【Swift】split(separator: ) vs components(separatedBy:)

2つあるけど、どっちを使うか。

どっちから使っていくか。


"1 2 3".split(separator: " ")
// ["1", "2", "3"]

"1 2 3".components(separatedBy: " ")
// ["1", "2", "3"]

split のほうが短いし、分かりやすくね?

 

■ 最初と最後の区切り文字


" 1 2 3 ".split(separator: " ")
// ["1", "2", "3"]

" 1 2 3 ".components(separatedBy: " ")
// ["", "1", "2", "3", ""]

 

■ 連続する区切り文字


"1  2  3".split(separator: " ")
// ["1", "2", "3"]

"1  2  3".components(separatedBy: " ")
// ["1", "", "2", "", "3"]

 

■ 割れない場合


"123".split(separator: " ")
// ["123"]

"123".components(separatedBy: " ")
// ["123"]

"1 2 3".split(separator: "23")
// ["1 2 3"]

"1 2 3".components(separatedBy: "23")
// ["1 2 3"]

"1 2 3".split(separator: "  ")
// ["1 2 3"]

"1 2 3".components(separatedBy: "  ")
// ["1 2 3"]

 

■ 空文字で割る


"123".split(separator: "")
// ["1", "2", "3"]

"123".components(separatedBy: "") 
// ["123"]

"1 2 3".split(separator: "")
// ["1", " ", "2", " ", "3"]

"1 2 3".components(separatedBy: "") 
// ["1 2 3"]

" 1 2 3 ".split(separator: "")
// [" ", "1", " ", "2", " ", "3", " "]

" 1 2 3 ".components(separatedBy: "") 
// [" 1 2 3 "]

"  1  2  3  ".split(separator: "")
// [" ", " ", "1", " ", " ", "2", " ", " ", "3", " ", " "]

"  1  2  3  ".components(separatedBy: "")
// ["  1  2  3  "]

 

■ 空文字を分割


"".split(separator: " ")
// []

"".components(separatedBy: " ")
// [""]

"".split(separator: "23") 
// []

"".components(separatedBy: "23")
// [""]

"".split(separator: "  ")
// []

"".components(separatedBy: "  ")
// [""]

"".split(separator: "")
// []

"".components(separatedBy: "")
// [""]

 

■ それでも直感的に分かりづらいような


"123".split(separator: "123")
// []

"123".components(separatedBy: "123")
// ["", ""]

"123".split(separator: "23")
// ["1"]

"123".components(separatedBy: "23")
// ["1", ""]

"  1  2  3  ".split(separator: " ")
// ["1", "2", "3"]

"  1  2  3  ".components(separatedBy: " ")
// ["", "", "1", "", "2", "", "3", "", ""]

 

■ まとめ

それぞれ他にも引数はいろいろあるようですが、


components(separatedBy:)

のほうがはっきり明快で分かりやすいように思えますが。


"  1  2  3  "
  .split(separator: " ")   // ["1", "2", "3"]
  .joined(separator: " ")  // "1 2 3"

"  1  2  3  "
  .components(separatedBy: " ") // ["", "", "1", "", "2", "", "3", "", ""]
  .joined(separator: " ")       // "  1  2  3  "

将来的にどうなるのか。

👉 【Swift】なんとなくメソッド名が長い気がする hatena-bookmark
👉 swift - component(separatedBy:) versus .split(separator: ) - Stack Overflow hatena-bookmark


【SwiftUI】ストップウォッチ つくってみる ⏱

非同期処理 や Observable クラスの確認です。



Timer クラスというのがあるようなので

それもやって比較してみました。



 

⏱ @Obserevable ってシンプル

マクロ @Observable ひとつだけで書けるようになったんですね !

👉 【SwiftUI】今どきの データモデル (Model data) のマクロ記述 📝 hatena-bookmark
👉 Migrating from the Observable Object protocol to the Observable macro | Apple Developer Documentation hatena-bookmark

 

⏱ 謎な部分

macOS で ビルトインの Timer クラスが30秒につき7秒ぐらいも遅れる。

なんでなんやろ ?

👉 Timer | Apple Developer Documentation hatena-bookmark