Swift for循环警告For Where Violation: `where` clauses are preferred over a single `if` inside a `for`. (for_where) - lidaoyuan/Notes GitHub Wiki

使用下面for循环出现警告 For Where Violation: where clauses are preferred over a single if inside a for. (for_where)

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

解决 1

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

2

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