首页 文章

SwiftLint:排除特定规则的文件

提问于
浏览
16

我想在我的.swiftlint.yml文件中做这样的事情:

force_cast:
  severity: warning # explicitly
  excluded:
    - Dog.swift

我有这个代码,我不喜欢我得到的force_try警告:

let cell = tableView.dequeueReusableCellWithIdentifier(Constants.dogViewCellReuseIdentifier,
                                                               forIndexPath: indexPath) as! DogViewCell

我想通过从规则中排除此文件来禁止此文件的警告 .

有没有办法做到这一点 ?

3 回答

  • 20

    好吧,如果您不希望某些特定规则应用于特定文件,您可以使用@Benno Kress提到的技术 . 为此,您需要在swift文件中添加注释,如下所示 .

    规则将被禁用,直到文件结束或者linter看到匹配的启用注释:

    // swiftlint:disable <rule1> 
    
       YOUR CODE WHERE NO rule1 is applied
    
    // swiftlint:enable <rule1>
    

    也可以通过配置swiftlint来跳过某些文件 . 在您将运行SwiftLint的目录中添加“ .swiftlint.yml ”文件 .

    添加以下内容以排除某些文件 . 让我们说file1,file2 ......等

    excluded: 
      - file1
      - file2
      - folder1
      - folder1/ExcludedFile.swift
    

    要禁用某些规则,请将以下内容添加到同一个“ .swiftlint.yml ”文件中 .

    disabled_rules: # rule identifiers to exclude from running
      - colon
      - comma
      - control_statement
    

    有关更多信息,请参阅以下链接 .

    https://swifting.io/blog/2016/03/29/11-swiftlint/

    https://github.com/realm/SwiftLint#disable-rules-in-code

  • 6

    您可以在文件的开头写入 // swiftlint:disable force_cast ,在该文件中您要抑制此规则的警告 . 它会被禁用,直到文件结尾或添加行 // swiftlint:enable force_cast .

    资料来源:https://github.com/realm/SwiftLint#disable-rules-in-code

  • 5

    我摆脱 force_cast

    Step 1:

    cd path-to-your-project
    

    Step 2:

    touch .swiftlint.yml
    

    Step 3: 打开.swiftlint.yml并添加

    disabled_rules: # rule identifiers to exclude from running
     - force_cast
    

    enter image description here

    参考 - https://github.com/realm/SwiftLint#disable-rules-in-code

相关问题