Swiftlint: Nesting rule and static variables

Created on 21 Apr 2017  ·  3Comments  ·  Source: realm/SwiftLint

class SomeClass {
   func update(badges: Any?) {
        func process(badges: [String: Any]?) {
            struct LastValues {
                static var activities: Int?
                static var chats: Int?
            }

I need static variables in this function and the only way that I know is to use struct. But I get warning Nesting Violation: Types should be nested at most 1 level deep (nesting)
So is there a way how to use static variables and avoid this warning?

question

Most helpful comment

The nesting rule exists for two reasons:

  1. to avoid overly-nested types in an API
  2. to avoid overly-indented statements (aka pyramid of doom)

I don't think that either of these situations apply here.

Ideally we would adapt the rule to not trigger when types are defined in functions since those are only available within the scope of that function.

All 3 comments

iMO you shouldn't change your code to please SwiftLint if you think this nesting is needed. You can disable a rule in code to ignore this warning as shown in the README.

The nesting rule exists for two reasons:

  1. to avoid overly-nested types in an API
  2. to avoid overly-indented statements (aka pyramid of doom)

I don't think that either of these situations apply here.

Ideally we would adapt the rule to not trigger when types are defined in functions since those are only available within the scope of that function.

Since that is already tracked in #1151, I'm closing this.

Was this page helpful?
0 / 5 - 0 ratings