2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Swift] 文字列、配列、辞書の空判定には.countではなく.isEmptyを使うべし

Last updated at Posted at 2022-04-30
// Bad
"".count == 0
[Int]().count == 0
[String: String]().count == 0

// Good
"".isEmpty
[Int]().isEmpty
[String: String]().isEmpty

<理由>
公式リファレンスにて、.isEmpryの使用が推奨されています。
.countの取得は、計算量がO(n)となる場合があるから、だそうです。

<補足>
SwiftLintにおいては、.countとの比較と同様にリテラルとの比較も警告対象となります。

// Bad
myString == ""
myArray == []
myDict == [:]

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?