如何修复 'Line Length Violation: Line should be 120 characters or less' - SwiftLint

How to fix 'Line Length Violation: Line should be 120 characters or less' - SwiftLint

如何解决行长违规问题?

由于行长违规而不允许的警告消息的相关部分:message: NSLocalizedString("\nYou will be requested to Use %@ to Sign In. %@ doesn't share any information about you. The permission is required to post your Live Video.", ⚠ 行应为 120 个字符或更少:当前为 208 个字符(line_length)

缩短线路:

message: NSLocalizedString(
    ["\nYou will be requested to Use %@ to Sign In. ",
    "%@ doesn't share any information about you. The ",
    "permission is required to post your Live Video."].joined()
)

或更好,使用 vacawama 的 multi-line 解决方案:

let message = 
    """

    You will be requested to Use %@ to Sign In. \
    %@ doesn't share any information about you. \
    The permission is required to post your Live Video.
    """

这是一个通用的解决方案,但并不真正适合 NSLocalizedString,因为它破坏了扫描本地化字符串的工具,例如 genstrings

您的另一个解决方案是通过在紧接在之前的行上添加禁用来关闭该行的警告:

// swiftlint:disable:next line_length

有关禁用 swiftlint 规则的完整详细信息,请参阅 Disable rules in code

在这种情况下,只需将 line_length 规则更新为 ignores_interpolated_strings,如下所示:

line_length:
  warning: 120
  ignores_function_declarations: true
  ignores_comments: true
  ignores_interpolated_strings: true
  ignores_urls: true

并确保您使用的是 swiftlint 的最新版本(added 就在几周前)

此行添加到 .swiftlint.yml 规则文件中,它对我有用

# implicitly 
line_length: 110