You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftLint排除目录与禁用规则失效问题求助

Troubleshooting SwiftLint Configuration Issues

Let’s tackle your SwiftLint problems one by one:

1. Fix Invalid Syntax in .swiftlint.yml

Looking at your config file, several entries in disabled_rules use Chinese en dashes (–) instead of English hyphens (-). SwiftLint can’t recognize these, so those rules aren’t actually being disabled. Here’s the corrected section:

disabled_rules: # 关闭默认启用的规则
  - colon
  - comma
  - control_statement
  - compiler_protocol_init
  - cyclomatic_complexity
  - file_length
  - force_cast
  - function_body_length
  - function_parameter_count
  - identifier_name
  - multiple_closures_with_trailing_closure
  - notification_center_detachment
  - line_length
  - trailing_whitespace
  - type_body_length
  - type_name
  - todo
  - shorthand_operator

2. Ensure .swiftlint.yml is in the Correct Location

Your excluded paths (like Pods) will only work if the config file is placed directly in your project root directory (the same folder as your .xcodeproj and Pods folder). If it’s nested elsewhere, SwiftLint won’t resolve relative paths correctly.

To test if the config works, run this in your project root terminal:

swiftlint lint --config .swiftlint.yml

Check if the output ignores the Pods directory and skips your disabled rules.

3. Resolve Conflicting SwiftLint Installations

Installing via both Homebrew and CocoaPods can cause version mismatches or unexpected behavior. Pick one method and stick with it:

Option A: Use Homebrew

  • Remove pod 'SwiftLint' from your Podfile, then run pod install to uninstall the CocoaPods version.
  • Keep your Build Phase script as:
    export PATH="$PATH:/opt/homebrew/bin"
    if which swiftlint > /dev/null; then
      swiftlint
    else
      echo "warning: SwiftLint not installed"
    fi
    
  • Verify the installation with brew list swiftlint to confirm it’s present.

Option B: Use CocoaPods

  • Uninstall the Homebrew version with brew uninstall swiftlint.
  • Ensure pod 'SwiftLint' is in your Podfile and you’ve run pod install.
  • Use this Build Phase script (it points directly to the Pods-installed SwiftLint):
    "${PODS_ROOT}/SwiftLint/swiftlint"
    

4. Adjust Build Phase Execution Order

Make sure your SwiftLint script runs before the "Compile Sources" phase in Xcode. If it runs after compilation, you might not see lint errors in the right context. To fix this:

  1. Open your project’s target settings → "Build Phases".
  2. Drag the SwiftLint script phase above "Compile Sources".

5. Double-Check Rule Names

Confirm all rule names in your config match official SwiftLint rules. You can list all valid rules with:

swiftlint rules

Typos here will prevent rules from being disabled/enabled as expected.


内容的提问来源于stack exchange,提问作者karthikeyan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 13:04:08