SwiftLint:排除特定规则的文件

时间:2016-09-23 16:46:34

标签: swiftlint

我正在尝试在我的.swiftlint.yml文件中执行类似的操作:

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

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

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

我想通过从规则中排除此文件来取消此文件的警告。

有办法吗?

4 个答案:

答案 0 :(得分:33)

好吧,如果您不希望某些特定规则应用于特定文件,您可以使用@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

答案 1 :(得分:10)

我摆脱 force_cast

第1步:

cd path-to-your-project

第2步:

touch .swiftlint.yml

第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

答案 2 :(得分:9)

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

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

答案 3 :(得分:1)

通过从要运行SwiftLint的目录中添加.swiftlint.yml文件来配置SwiftLint。这是您可以在.swiftlint.yaml文件中使用的完整选项集

disabled_rules: # rule identifiers to exclude from running
  - colon
  - comma
  - control_statement
opt_in_rules: # some rules are only opt-in
  - empty_count
  # Find all the available rules by running:
  # swiftlint rules
included: # paths to include during linting. `--path` is ignored if present.
  - Source
excluded: # paths to ignore during linting. Takes precedence over `included`.
  - Carthage
  - Pods
  - Source/ExcludedFolder
  - Source/ExcludedFile.swift
  - Source/*/ExcludedFile.swift # Exclude files with a wildcard
analyzer_rules: # Rules run by `swiftlint analyze` (experimental)
  - explicit_self

# configurable rules can be customized from this configuration file
# binary rules can set their severity level
force_cast: warning # implicitly
force_try:
  severity: warning # explicitly
# rules that have both warning and error levels, can set just the warning level
# implicitly
line_length: 110
# they can set both implicitly with an array
type_body_length:
  - 300 # warning
  - 400 # error
# or they can set both explicitly
file_length:
  warning: 500
  error: 1200
# naming rules can set warnings/errors for min_length and max_length
# additionally they can set excluded names
type_name:
  min_length: 4 # only warning
  max_length: # warning and error
    warning: 40
    error: 50
  excluded: iPhone # excluded via string
identifier_name:
  min_length: # only min_length
    error: 4 # only error
  excluded: # excluded via string array
    - id
    - URL
    - GlobalAPIKey
reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, junit, html, emoji, sonarqube, markdown)

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