【问题标题】:How do I create .swiftlint.yml file & where I need to put it?如何创建 .swiftlint.yml 文件以及我需要把它放在哪里?
【发布时间】:2017-08-23 21:22:12
【问题描述】:

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

所以请建议我如何进行?

【问题讨论】:

    标签: swiftlint


    【解决方案1】:

    如果您使用的是终端:

        cd your_project_directory
        touch .swiftlint.yml
    

    【讨论】:

    • 它是 your_project_directory 中的隐藏文件,要查看它,您应该启用您的目录以显示隐藏文件。
    • 要在 Mac OS Sierra 中显示或隐藏隐藏文件,您可以使用 cmd + shift + ..
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      在您的项目主目录中创建此文件,名称应为.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)
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 视频不可用。
        【解决方案5】:

        我建议你阅读documentation

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

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

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

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

        swiftlint 规则 > swiftlint_rules.txt

        并且有非常宽的屏幕。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-08-07
          • 1970-01-01
          • 1970-01-01
          • 2014-06-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多