Swiftlint 警告:对于 Where 违规:`where` 子句优于 `for` 中的单个 `if`。 (for_where)
Swiftlint warning : For Where Violation: `where` clauses are preferred over a single `if` inside a `for`. (for_where)
我正在使用 swift 进行应用程序开发并使用 Swift lint。但我收到有关以下代码的警告:
for settingsKeys in searchResults {
if settingsKeys.key == settingsObject.key {
settingsKeys.value = settingsObject.value
try context.save()
}
}
特此附上截图:
没有可用的自动修复选项,那么如何消除此警告?
您的 swiftlint 配置首选的语法是:
for settingsKeys in searchResults where settingsKeys.key == settingsObject.key {
settingsKeys.value = settingsObject.value
try context.save()
}
与
相似
for settingsKeys in (searchResults.filter { [=11=].key == settingsObject.key }) {
settingsKeys.value = settingsObject.value
try context.save()
}
如果你知道只有一个结果是相同的key
,你可能会直接使用
if let settingsKeys = (searchResults.first { [=12=].key == settingsObject.key }) {
settingsKeys.value = settingsObject.value
try context.save()
}
看起来它希望 where
成为 for
的一部分…
for settingsKeys in searchResults where settingsKeys.key == settingsObject.key {
settingsKeys.value = settingsObject.value
try context.save()
}
我正在使用 swift 进行应用程序开发并使用 Swift lint。但我收到有关以下代码的警告:
for settingsKeys in searchResults {
if settingsKeys.key == settingsObject.key {
settingsKeys.value = settingsObject.value
try context.save()
}
}
特此附上截图:
没有可用的自动修复选项,那么如何消除此警告?
您的 swiftlint 配置首选的语法是:
for settingsKeys in searchResults where settingsKeys.key == settingsObject.key {
settingsKeys.value = settingsObject.value
try context.save()
}
与
相似for settingsKeys in (searchResults.filter { [=11=].key == settingsObject.key }) {
settingsKeys.value = settingsObject.value
try context.save()
}
如果你知道只有一个结果是相同的key
,你可能会直接使用
if let settingsKeys = (searchResults.first { [=12=].key == settingsObject.key }) {
settingsKeys.value = settingsObject.value
try context.save()
}
看起来它希望 where
成为 for
的一部分…
for settingsKeys in searchResults where settingsKeys.key == settingsObject.key {
settingsKeys.value = settingsObject.value
try context.save()
}