Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How to fix Line Length Violation?

Relevant part of alert message that isn't allowed due to Line Length Violation: 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.", ⚠ Line should be 120 characters or less: currently 208 characters (line_length)

like image 253
Yatko Avatar asked Feb 24 '18 17:02

Yatko


People also ask

How do I turn off SwiftLint rules?

Disable rules in code Rules can be disabled with a comment inside a source file with the following format: // swiftlint:disable <rule1> [<rule2> <rule3>...] The rules will be disabled until the end of the file or until the linter sees a matching enable comment: // swiftlint:enable <rule1> [<rule2> <rule3>...]

How do I fix line length violations in Swift?

How to fix Line Length Violation? This looks like a SwiftLint message. Well, shorten the line or disable the warning (clearly explained at github.com/realm/SwiftLint/blob/master/…)

How does SwiftLint work?

SwiftLint is an open-source tool to enforce Swift style and conventions. It is developed by Realm. You can set your coding style rules and force them during development. SwiftLint has a command-line tool, Xcode plugin, AppCode, and Atom integration.


3 Answers

In this case just update your line_length rule with ignores_interpolated_strings like this:

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

and make sure you are using last version of swiftlint (it was added just a few weeks ago)

like image 137
Ilya Ilin Avatar answered Oct 18 '22 03:10

Ilya Ilin


Make the line shorter:

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()
)

or better, using vacawama's multi-line solution:

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.
    """

That's a generic solution, but isn't really appropriate for NSLocalizedString because it breaks tools that scan for localized strings like genstrings.

Your other solution is to turn off the warning for that line by adding a disable on the line immediately before:

// swiftlint:disable:next line_length

See Disable rules in code for full details on disabling swiftlint rules.

like image 45
Rob Napier Avatar answered Oct 18 '22 04:10

Rob Napier


This line added in .swiftlint.yml rule file its works for me

# implicitly 
line_length: 110
like image 3
kishor soneji Avatar answered Oct 18 '22 04:10

kishor soneji