57

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

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

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

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

我想通过从规则中排除这个文件来禁止这个文件的警告。

有没有办法做到这一点 ?

4

6 回答 6

94

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

于 2017-08-14T19:26:19.060 回答
26

您可以// swiftlint:disable force_cast在要禁止此规则警告的文件的开头写入。在文件末尾或添加该行之前,它会被禁用// swiftlint:enable force_cast

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

于 2016-12-29T11:15:47.127 回答
12

我只是摆脱了 force_cast

步骤1:

cd path-to-your-project

第2步:

touch .swiftlint.yml

第三步: open .swiftlint.yml添加规则

disabled_rules: # rule identifiers to exclude from running
 - force_cast

在此处输入图像描述

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

于 2017-12-05T04:54:43.120 回答
1

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

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

于 2019-04-30T12:31:45.033 回答
0

您可以将新.swiftlint.yml文件添加到排除的文件夹并在那里覆盖规则 project_root/.swiftlint.yml

opt_in_rules:
  force_unwrapping
.. and other rules

并且在

project_root/your_excluded_folder/.swiftlint.yml

disabled_rules:
  - force_unwrapping

然后 force_unwrapping将不会应用于your_excluded_folder所有子文件夹

子文件夹中的配置文件将“覆盖”根配置文件中的规则。

这对于例如单元测试文件夹很有用

于 2021-12-03T15:45:29.943 回答
0

也许这是一个更好的方法:

guard let cell = tableView.dequeueReusableCellWithIdentifier(Constants.dogViewCellReuseIdentifier,
                                                               forIndexPath: indexPath) as? DogViewCell else {
return UITableviewCell()
}
于 2021-11-20T12:42:35.870 回答