LoginSignup
6
5

More than 3 years have passed since last update.

For Where Violation: `where` clauses are preferred over a single `if` inside a `for`. (for_where) の警告が出た時の処理

Last updated at Posted at 2019-10-10

for...in...{ if ... == ...} をwhereでスッキリさせる。

for musicItem in musics {
     if musicItem.title == favoriteTitle {
         favoriteList.append(musicItem)
     }
 }

こんな感じで、配列に入った値を一つ一つに分解して、その分解した値のkeyで処理をしたい場合、whereを使えって警告が出る。
その変換


for musicItem in musics where musicItem.title == favoriteTitle {
      favoriteList.append(musicItem)
}

これでOK。
もっとスッキリさせるためには次


for musicItem in (musics.filter { $0.title == favoriteTitle }) {
  favoriteList.append(musicItem)
}

参考:
https://stackoverflow.com/questions/45590544/swiftlint-warning-for-where-violation-where-clauses-are-preferred-over-a-si

6
5
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
6
5