swiftlint全局安装和使用

1、安装
通过homebrew命令全局安装:

brew install swiftlint

2、打开Xcode,在项目中添加编译脚本:

if which swiftlint >/dev/null; then
  swiftlint
else
  echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
iShot_2023-03-17_14.34.37.png

3、添加和配置.swiftlint.yml文件
在项目根目录创建.swiftlint.yml文件,内容大致如下:

excluded: # 执行 linting 时忽略的路径。 优先级比 `included` 更高。
  - Carthage
  - Pods

line_length:
  warning: 350
  error: 450
  ignores_function_declarations: true
  ignores_comments: true

function_body_length: # 函数体长度
  warning: 300
  error: 350

identifier_name:
  min_length: # 只有最小长度
    error: 1 # 只有错误

type_body_length: # 类的长度
  warning: 800
  error: 1200

file_length: # 文件长度
  warning: 1000
  error: 1500

cyclomatic_complexity: # 代码复杂度,默认为10
  warning: 30
  error: 35

force_cast: warning # 强制转换(代码中存在一些前面通过if判断过类型,后面做的强制转换的代码)
force_try: warning # try语句判断

disabled_rules: # 执行时排除舍弃的规则
  - trailing_whitespace # 每一个空行不能有空格,会与Xcode换行后自动对齐生成的空格冲突,建议排除掉加。
  - identifier_name # 命名规则必须按照驼峰原则(可能model中的某些字段与json字段命名冲突,建议排除掉)
  - type_name # 类型命名规则限制,以大写字母开头,且长度在1到20个字符之间
  - shorthand_operator # 使用+= , -=, *=, /=  代替 a = a + 1

4、编译项目,即可看到swiftlint的警告或报错。


iShot_2023-03-17_14.39.31.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容