How to Fix '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

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.

Swift – Function Body Length Violation or Line Length Violation


Option 1: adapt your layout or your design

Your problem seems to be caused by a long array of English words in the source code.

A simple way between one item per line and one line with all the items, would be to spread this data on several lines:

1492 chars broken down into lines of 120 gives 13 lines. If you take into consideration the average English word length of 4.7 (in your case an average unsecable token length of 6.7 with the enclosing quotes, you might to have to shift some of the wors on extra lines. DOing the math, in average, the extra lines have to contain 88 chars extra (6.7*13) so a 14th line. You have still plenty of margins in view of the 175 line limits ;-)

An even better approach, would be to store data in a file and load the data into an array dynamically at run-time. Not only won't you overweight your source code, but in addition, you'd facilitate maintenance of the list with new words, as well as internationalization.

Option 2: configure your linter

The relativeley small line length and body length limits are not about syntax or compiler limitations. It's static analysis rules intended to challenge you write more readable code.

You can disable the rules:

  • at project level, in the file .swiftlint.yml
  • in the source code, with a comment // swiftlint:disable <rule1> [<rule2> <rule3>...]

You can find practical examples with the line_length in this SO question

How can I turn off line length violations for an entire project when using SwiftLint

Add the line_length rule into the disabled_rules section of your .swiftlint.yml config file

disabled_rules:
- line_length

SwiftLint -Disable Line Length Rules in a specific file

The rule name is file_length, so you have to disable this rule:

// swiftlint:disable file_length

import UIKit

class MyClass: UIViewController {

}

Note: // swiftlint:enable <rule> is for cases when you want to ignore a specific rule only in a small code block (like a single func). If you'd like to disable a rule in file scope, there is no need to enable anything.

See Swiftlint docs.

What exactly does 'Type Body Length' mean in Swiftlint?

type_body_length violation means that the class has too many lines in it. I dont think it counts extensions, comments or whitespace

Type name should only contain alphanumeric characters, start with an uppercase character and span between 3 and 40 characters in length.

The rules documentation linked here and above also gives examples of what would and wouldn't be accepted (Triggering & Non Triggering). - Edit suggested by @GoodSp33d, thanks



Related Topics



Leave a reply



Submit