33

我想在我的 Swift 项目中使用 Swiftlint。我按照 Realm 的说明安装了 Swiftlint brew install swiftlint。此外,我面临创建.swiftlint.yml文件的问题。

所以请建议我如何进行?

4

5 回答 5

40

如果您使用的是终端:

    cd your_project_directory
    touch .swiftlint.yml
于 2017-11-30T13:15:49.763 回答
10

我发现这个视频很有帮助: https ://www.youtube.com/watch?v=3MAlqOVIAwI

您可以在 XCode 中创建一个 .swiftlint.yml 并将其保存在您的项目目录中。只需选择文件 -> 新建 -> 文件 -> 空

于 2017-04-01T18:22:05.857 回答
5

在您的项目主目录中创建此文件,名称应为.swiftlint.yml

文件示例

disabled_rules: # rule identifiers to exclude from running
  - colon
  - comma
  - control_statement
  - identifier_name #rule for checking variable conditions (Upper case , lower case , underscore )
  - force_cast
  - shorthand_operator

cyclomatic_complexity:
  warning: 25 # two nested ifs are acceptable
  error: 50   # six nested ifs shows warning, 6 causes compile error


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
  - AppFolder\ App/Class/*
 # - AppFolder\ App/ViewController/* //Enabled for this

analyzer_rules: # Rules run by `swiftlint analyze` (experimental)
  - explicit_self

# configurable rules can be customised 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: 200
# they can set both implicitly with an array

type_body_length:
  - 300 # warning
  - 600 # error
# or they can set both explicitly

file_length:
  warning: 500
  error: 2500

function_body_length:
  - 200 #warning
  - 300 #error

# 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
  allowed_symbols: ["_"] # these are allowed in type names
identifier_name:
  min_length: # only min_length
    error: 4 # only error
  excluded: # excluded via string array
    - id
    - URL
    - GlobalAPIKey

identifier_name:
#  allowed_symbols: "_"
  max_length:
    warning: 45
    error: 60
  min_length:
    warning: 1


reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, junit, html, emoji, sonarqube, markdown)
于 2020-01-15T11:24:02.727 回答
4

我通过 cocoapods 安装 swift lint,然后.swiftlint.yml在根项目目录中添加具有名称的新文件。有关更多详细信息,请访问 youtube 链接。
我发现此视频很有帮助:https ://www.youtube.com/watch?v=cEA9BDVbjfI或 https://www.youtube.com/watch?v=3MAlqOVIAwI

于 2018-01-02T11:27:13.680 回答
-4

我建议您阅读文档

通过从运行 SwiftLint 的目录中添加一个 .swiftlint.yml 文件来配置 SwiftLint。

我建议您的项目或源根目录。

下面是一个广泛的示例文件;从那里开始。

至于可用的规则和默认值,除了运行似乎没有很好的文档

swiftlint 规则 > swiftlint_rules.txt

并且有非常宽的屏幕。

于 2017-03-30T08:27:08.897 回答