38

我正在使用 swift 进行应用程序开发并使用 Swift lint。但我收到有关以下代码的警告:

for settingsKeys in searchResults {

        if  settingsKeys.key == settingsObject.key {
          settingsKeys.value = settingsObject.value
          try context.save()
        }
      }

特此附上截图:

在此处输入图像描述

没有可用的自动修复选项,那么如何消除此警告?

4

2 回答 2

99

您的 swiftlint 配置首选的语法是:

for settingsKeys in searchResults where settingsKeys.key == settingsObject.key {
    settingsKeys.value = settingsObject.value
    try context.save()
}

哪个类似于

for settingsKeys in (searchResults.filter { $0.key == settingsObject.key }) {
    settingsKeys.value = settingsObject.value
    try context.save()
}

如果你知道只有一个结果相同key,你可以直接使用

if let settingsKeys = (searchResults.first { $0.key == settingsObject.key }) {
    settingsKeys.value = settingsObject.value
    try context.save()
}
于 2017-08-09T12:22:10.593 回答
4

看起来它希望where成为……的一部分for。</p>

for settingsKeys in searchResults where settingsKeys.key == settingsObject.key {
    settingsKeys.value = settingsObject.value
    try context.save()
}
于 2017-08-09T12:22:57.637 回答