如何修复'行长度违规:行应该是120个字符或更少' - SwiftLint

Yat*_*tko 2 swift 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)

Rob*_*ier 14

缩短线条:

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()
)
Run Code Online (Sandbox Code Playgroud)

或者更好,使用vacawama的多线解决方案:

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.
    """
Run Code Online (Sandbox Code Playgroud)

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

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

// swiftlint:disable:next line_length
Run Code Online (Sandbox Code Playgroud)

有关禁用 swiftlint规则的完整详细信息,请参阅禁用代码中的规则.

  • 如果以反斜杠结束行,则不会引入换行符. (4认同)
  • (我这样说是因为长期倡导“一种真正的 Swift 风格”,这样 clang-format 将强制执行一种完全相同的 Swift 代码布局方式。即使我有自己的方式,有时也会忽略一个 linter。http://openradar.appspot.com/radar?id=5886836526284800) (2认同)

Ily*_*lin 9

在这种情况下,只需使用以下命令更新您的line_length规则ignores_interpolated_strings

line_length:
  warning: 120
  ignores_function_declarations: true
  ignores_comments: true
  ignores_interpolated_strings: true
  ignores_urls: true
Run Code Online (Sandbox Code Playgroud)

并确保您使用的是最新版本swiftlint(仅在几周前添加

  • 它不起作用:(它尝试将相同的内容放入 .yml 文件中,并且再次出现相同的警告,只有以下内容到目前为止正在工作 line_length: 200 我不希望所有行都这样 (3认同)