【问题标题】:Switch case formatting issue with SwiftLintSwiftLint 的切换大小写格式问题
【发布时间】:2021-05-10 00:22:18
【问题描述】:

当我执行 Cntrl + i 时,我的开关盒会自动格式化,如下所示

switch someBool {
    ↓case true:
        print("success")
    ↓case false:
        print("failed")
}

但它会引发 lint 警告 Switch and Case Statement Alignment Violation: Case statements should vertically align with their enclosing switch statement. (switch_case_alignment)

我已经手动格式化如下

switch someBool {
case true:
    print('red')
case false:
    print('blue')
}

但是一旦我这样做了Cntrl+I

欢迎提出任何建议。谢谢。

【问题讨论】:

  • 你的目标是什么?要更改 Xcode 的自动格式以匹配特定的 SwiftLint 设置?让 SwiftLint 的建议与 Xcode 的自动格式匹配?让 SwiftLint 根本不关心开关格式?还是别的什么?

标签: swift switch-statement swiftlint


【解决方案1】:

您可以使用以下复选框在 Xcode 上调整该设置。我认为默认情况下它是未选中的,它应该符合 SwiftLint 的默认规则。

【讨论】:

  • 解决方案不起作用。
【解决方案2】:

当您遇到 SwiftLint 规则违规时,您可以随时访问此页面了解更多信息:https://realm.github.io/SwiftLint/switch_case_alignment.html

这向您展示了如何解决它。如果您认为无法轻松修复它,您可以设置一个豁免来告诉 SwiftLint 忽略。

因此,您只需将此注释放在您的 switch 语句上方的行中:

// swiftlint:disable switch_case_alignment

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 2016-03-05
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多