SwiftLint排除目录与禁用规则失效问题求助
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 runpod installto 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 swiftlintto 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 runpod 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:
- Open your project’s target settings → "Build Phases".
- 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

