8

I have a function on Xcode (swift) that returns a very long array, that is one word per line. Like

static func en() -> [String] {
    [
        "a",
        "about",
        "above",
        "after",
        "again",
        "against",
        "all",
        "am",
        ...

When I try to compile this I get:

Function Body Length Violation: Function body should span 40 lines or less excluding comments and whitespace: currently spans 175 lines (function_body_length)

if I try to remove the line feeds and add spaces instead I get this

Line Length Violation: Line should be 120 characters or less: currently 1492 characters (line_length)

I think the problem is SwiftLint.

How do I solve this? How do I disable this error.

Duck
  • 34,902
  • 47
  • 248
  • 470
  • What are you trying to achieve? – Rob Nov 15 '20 at 10:42
  • Maybe having several items on several lines? 1492 chars in lines of 120 gives 13 lines. If you take into consideration the average English word length of 4.7, resulting in an average unsecable token length of 6.7, avoiding breaking tokens could add in average 88 chars extra so a 14th line. You have still plenty of margins in view of the 175 line limits. But I’ll not answer that because independently of this underlying code, it is interesting to see if and how to circumvent these limits. – Christophe Nov 15 '20 at 10:48
  • As they appear to be errors from SwiftLint you can disable the linter In the file for those problem areas with comment markup e.g `// swiftlint:disable function_body_length` . See https://github.com/realm/SwiftLint – Warren Burton Nov 15 '20 at 10:49
  • In addition, you lay find some additional infos on customizing rules here: https://stackoverflow.com/q/48965916/3723423 – Christophe Nov 15 '20 at 10:53
  • @WarrenBurton - where do I add this? I am talking about Xcode here. – Duck Nov 15 '20 at 11:35
  • You obviously have SwiftLint installed and executing for your project, if you follow the link supplied by Warren you see that SwiftLint is well documented and that there are clear examples on how to do this. – Joakim Danielson Nov 15 '20 at 11:39
  • @JoakimDanielson... I obviously have SwiftLint installed? I never installed that. I did remove it and Xcode complained that SwiftLint was missing. I guess Xcode installs this crap by itself. – Duck Nov 15 '20 at 11:55

3 Answers3

7

Add this before the function definition to ignore swiftlint function body length rule.

// swiftlint:disable function_body_length
Ishaan Gupta
  • 101
  • 1
  • 2
6

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

Christophe
  • 68,716
  • 7
  • 72
  • 138
0

I guess you can put your array in a json file and decode it like that.

if let url = Bundle.main.url(forResource: "Filename", withExtension: "json") {
        do {
            let data = try Data(contentsOf: url)
            let array = try! JSONDecoder().decode(YourModel.self, from: data)
            // deal with it.
        } catch {
            print("error",error)
        }
    }
iRamzy_
  • 1
  • 1
  • 1