irpas技术客

iOS swift swiftLint m1 安装+规则_『木辛』「月月鸟」_`if let` shorthand

网络投稿 5029

iOS swiftLint m1 安装+规则 m1安装

终端不使用Rosetta

brew install swiftlint

Cocapods 安装

pod 'SwiftLint'

打开项目xcode,新建两个脚本命令

这里分别复制

if which swiftlint >/dev/null; then swiftlint #echo "skip" else echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint" fi

"${PODS_ROOT}/SwiftLint/swiftlint"

可以运行,但是全部的规则都用上了。

需要新建规则 .swiftlint.yml

在项目根目录和Profile同级 终端敲touch .swiftlint.yml

这文件是隐藏的。command + shift + .在更目录显示,可先复制

disabled_rules: # 执行时排除掉的规则 - colon - comma - control_statement opt_in_rules: # 一些规则仅仅是可选的 - empty_count - missing_docs # 可以通过执行如下指令来查找所有可用的规则: # swiftlint rules included: # 执行 linting 时包含的路径。如果出现这个 `--path` 会被忽略。 excluded: # 执行 linting 时忽略的路径。 优先级比 `included` 更高。 - Carthage - Pods # 可配置的规则可以通过这个配置文件来自定义 # 二进制规则可以设置他们的严格程度 force_cast: warning # 隐式 force_try: severity: warning # 显式 # 同时有警告和错误等级的规则,可以只设置它的警告等级 # 隐式 line_length: 110 # 可以通过一个数组同时进行隐式设置 type_body_length: - 300 # warning - 400 # error # 或者也可以同时进行显式设置 file_length: warning: 500 error: 1200 # 命名规则可以设置最小长度和最大程度的警告/错误 # 此外它们也可以设置排除在外的名字 type_name: min_length: 4 # 只是警告 max_length: # 警告和错误 warning: 40 error: 50 excluded: iPhone # 排除某个名字 identifier_name: min_length: # 只有最小长度 error: 4 # 只有错误 excluded: # 排除某些名字 - id - URL - url - GlobalAPIKey reporter: "xcode" # 报告类型 (xcode, json, csv, checkstyle)

第三方库的报错不管

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

重新编译规则

swiftlint autocorrect 常会犯错规则提示

https://realm.github.io/SwiftLint/rule-directory.html

class_delegate_protocol #protocol和delegate的命名区分

当协议 protocol的命名为xxdelgate一定要继承某protocol,不是delegate结尾不会报错

closing_brace #闭包间距 colon #名称间距 关于:

Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)

:前不空格,后需要空一格

let swift: String? comma #参数间距 关于,

,号前不需要空格,后需要空一格

comment_spacing #注释

Comment Spacing Violation: Prefer at least one space after slashes for comments. (comment_spacing)

compiler_protocol_init #协议的init方法不推荐使用

有点反人类

computed_accessors_order #get set位置不能错

先get 后 set

control_statement #判断语句

判断语句 if, for, guard, switch, while, catch

判断语句最外层不需要添加()

custom_rules 添加新规则 custom_rules: pirates_beat_ninjas: # 规则标识符 name: "Pirates Beat Ninjas" # 规则名称,可选 regex: "([nN]inja)" # 匹配的模式 match_kinds: # 需要匹配的语法类型,可选 - comment - identifier message: "Pirates are better than ninjas." # 提示信息,可选 severity: error # 提示的级别,可选 no_hiding_in_strings: regex: "([nN]inja)" match_kinds: string

实现

empty_count #判断数组的count == 0 用 isEmpty

现有问题是optional的数组直接用 count == 0 就不需要isEmpty

cyclomatic_complexity #循环复杂性 判断条件未知(可以删掉吧)

(不清楚)判断里太多判断会报

missing_docs # 类名 扩展类不要用public、open

Missing Docs Violation: public declarations should be documented. (missing_docs)

deployment_target #iOS适用版本 iOS 7.1以下会报 discouraged_direct_init #类初始化要带参数,不能直接初始化,

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-F7WE3NbC-1650783909680)(/Users/jtg_0705/Library/Application Support/typora-user-images/截屏2022-03-24 下午4.13.56.png)]

duplicate_enum_cases #枚举成员变量名重名 duplicate_imports # 重复含有的import类型 duplicated_key_in_dictionary_literal # 字典含有相同成员变量名 dynamic_inline #动态内联,基本用不上 empty_enum_arguments #空枚举论点 基本用不上

Case 语句后的参数不加括号

case .参数+()

empty_enum_arguments #参数为void不需要写void ,Xcode自带判别 empty_parentheses_with_trailing_closure #带有尾随闭包 empty_parentheses_with_trailing_closure # 尾随闭包不用写() file_length #代码行数

超过500行警告,超过1200行error

file_length: warning: 500 error: 1200 for_where #使用语法糖 for where

警告语法

for user in users { if user.id == 1 { return true } } force_cast #类型转换

不能强制类型转换

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1HcdW9ts-1650783909680)(/Users/jtg_0705/Library/Application Support/typora-user-images/截屏2022-03-24 下午5.08.22.png)]

force_try # 不建议强制 try! function_body_length #函数最大占用 默认配置:warning:40,error:100 可以删除吧 function_parameter_count #函数的参数多少 参数的多少: warning: 5, error: 8 generic_type_name #泛型名称规范

不能使用_ 第一个字母为大写, 字数不超过20行

identifier_name # 参数命名规范

使用小驼峰, private标识的前面可带_ 其他均报error

implicit_getter #只有get不能return include_language #敏感词汇 可删 inert_defer #defer放在函数底部,可删 is_disjoint #不相交函数。没用过 large_tuple # 元组尽量不要用(类型,类型)

leading_whitespace #缩进 建议不用

很恶心,下一行xcode自带缩进,然后就报警告了, control + i 就直接报错了

legacy_cggeometry_functions #遗留的CGRectGet legacy_constant #遗留的 CGPointZero 已经遗弃 legacy_constructor # 遗弃的构造器

ps: CGSizeMake(10, 10) 部分Xcode也会报错

legacy_hashing #重写hash 用 hash(into: ) 代替 hashValue

项目里没用到

legacy_random #用 类型.random 代替arc4random line_length #一行不能超过110个字 line_length: 110

部分delegate的api会超过110个 tableViewDelegate的某些api会有, AppDelegate会有

mark # 规范 // mark: - 其余都不规范 multiple_closures_with_trailing_closure #多个参数为闭包,尾随闭包要带形参(重要) nesting 不能有多层嵌套

func 不能超过4层,其他不能超过3层

no_fallthrough_only # switch fallthrough 用法 代替的break继续执行 no_space_in_method_call #函数不带空格

不允许 函数名+ 空格 + ()

notification_center_detachment #NotificationCenter删除自己的监听要在deinit执行

只有NotificationCenter.default.removeObserver(self)才会触发

nsobject_prefer_isequal # NSObject子类应该实现isEqual而不是==

就是继承NSObject不能用 func ==() {}

opening_brace # {} 括号外除了func外都要空一格

operator_whitespace #运算符函数空格

看不懂 函数用< | > 这种类型命名很少见

orphaned_doc_comment # 注释代码规范,/// 下不应该有//

private_over_fileprivate #外部定义用private不用fileprivate

private_unit_test #单元测试不要用private

基本用不上

protocol_property_accessors_order # 和 computed_accessors_order 一样,get set位置不能错 reduce_boolean # 数组 reduce不使用bool,使用0 代替 redundant_discardable_let #未用到的参数把let去掉

let _ = foo() 改为 _ = foo()

redundant_objc_attribute #本身自带@objc 把@objc去掉 @IBInspectable @IBAction @GKInspectable @NSManaged @IBDesignable redundant_optional_initialization # 冗余optional 值初始化

错误

var myIntValue: Int? = nil var myOptionalInt: Optional<Int> = nil redundant_set_access_control #冗余前缀,基本不会碰到

错误

private(set) private var foo: Int redundant_string_enum_value #当字符串枚举值都等于名称报警告 /// 这样子不会报错 private enum YourEnum: String { case first = "first" case second = "second" case third = "three" // 第三个不不相等不会报错 } redundant_void_return # 冗余空返回

如果函数没有返回值不用写 -> Void

return_arrow_whitespace #函数返回没有空格或多了空格

Returning Whitespace Violation: Return arrow and return type should be separated by a single space or on a separate line. (return_arrow_whitespace)

self_in_property_initialization # 闭包参数 调用self要用lazy

shorthand_operator # 自增减乘除使用语法糖

statement_position #else 与 } 之间有一个空格

Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration.

superfluous_disable_command # 当禁用规则不会在禁用区域触发违规时,SwiftLint“禁用”命令是多余的。如果您想记录命令,请使用“-” switch_case_alignment # switch case 对齐缩进

switch case control + i 解决

syntactic_sugar #swift数组语法糖

不推荐使用

Array<String> Dictionary<Int, String> Optional<Int> Swift.Array<String> todo # 不推荐使用todo (感觉可删)

推荐使用

// notaTODO: // notaFIXME:

其余的不推荐

// TODO: // FIXME: // TODO(note) // FIXME(note) /* FIXME: */ ... trailing_comma #尾随逗号

数组最后一个元素不加 ,

trailing_newline # 尾随换行符

xcode自带换行符?没碰过

trailing_semicolon #尾随分号 ;

Xcode 自己也会报错

trailing_semicolon #一行结束不要空格 type_body_length # 类行数大小 感觉都会超过 type_body_length: - 300 # warning - 400 # error type_name # 命名规范长度 type_name: min_length: 4 # 只是警告 max_length: # 警告和错误 warning: 40 error: 50 excluded: iPhone # 排除某个名字 unneeded_break_in_switch # switch 语句不需要写break unused_capture_list # 闭包里没有用到的引用 { [weak self] in }

Xcode 自带了

unused_closure_parameter # 没有用到参数

没有用到用 _解决

unused_control_flow_label #看不懂 可能是语法本身就错 unused_enumerated

当不使用索引或项目时,可以删除.enumerated()

不知道怎么实现,如果报错删掉.enumerated()

unused_optional_binding # 使用 != nil || if let 代替 if let _ =

xcode 自带不能这样写了

guard (scene as? UIWindowScene) != nil else { return } unused_setter_value #不使用setter值 自己set自己就不需要写 valid_ibinspectable # 不规范@IBInspectable

@IBInspectable 类型的view 给 xib使用可以动态更改某些属性,类似圆角设置

这些不规范看不懂,用到也少

vertical_parameter_alignment # 函数 参数换行对齐 (我觉得可以删)当参数太后就很麻烦

trailing_whitespace #一行后面不能有空格 vertical_whitespace #换行默认只允许1行

可以修改

vertical_whitespace: warning: 2 void_return # 建议使用 -> Void 代替 -> (). xctfail_message #XCTFail() 项目里没用到


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #IF #let #shorthand #iOS #SwiftLint #M1 #install