AnyObject Protocol

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
AnyObject Protocol lint 4.1.0

对于class-only protocols 优先使用AnyObject而不是Class

示例

非触发

  1. protocol SomeProtocol {}
  2. protocol SomeClassOnlyProtocol: AnyObject {}
  3. protocol SomeClassOnlyProtocol: AnyObject, SomeInheritedProtocol {}
  4. @objc protocol SomeClassOnlyProtocol: AnyObject, SomeInheritedProtocol {}

触发

  1. protocol SomeClassOnlyProtocol: class {}
  2. protocol SomeClassOnlyProtocol: class, SomeInheritedProtocol {}
  3. @objc protocol SomeClassOnlyProtocol: class, SomeInheritedProtocol {}

补充

定义委托时,我们让 protocol 继承自 AnyObject。这是由于,在 Swift 中,这表示这一个协议只能被应用于 class(而不是 struct 和 enum)。

实际上,如果让 protocol 不继承自任何东西,那也是可以的,这样定义的 Delegate 就可以被应用于 class 以及 struct、enum。由于 Delegate 代表的是遵循了该协议的实例,所以当 Delegate 被应用于 class 时,它就是 Reference type,需要考虑循环引用的问题,因此就必须要用 weak 关键字。

但是这样的问题在于,当 Delegate 被应用于 struct 和 enum 时,它是 Value type,不需要考虑循环引用的问题,也不能被使用 weak 关键字。所以当 Delegate 未限定只能用于 class,Xcode 就会对 weak 关键字报错:‘weak’ may only be applied to class and class-bound protocol types

那么为什么不使用 class 和 NSObjectProtocol,而要使用 AnyObject 呢?NSObjectProtocol 来自 Objective-C,在 pure Swift 的项目中并不推荐使用。class 和 AnyObject 并没有什么区别,在 Xcode 中也能达到相同的功能,但是官方还是推荐使用 AnyObject。

来源 wift Protocol 详解 - 协议&面向协议编程


Array Init

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
Array Init lint 3.0.0

将序列(sequence)转化为数组 优先使用Array(seq) 而不是 seq.map{$0}

示例

非触发

  1. Array(foo)
  2. foo.map { $0.0 }
  3. foo.map { $1 }
  4. foo.map { $0() }
  5. foo.map { ((), $0) }
  6. foo.map { $0! }
  7. foo.map { $0! /* force unwrap */ }
  8. foo.something { RouteMapper.map($0) }

触发

  1. foo.map({ $0 })
  2. foo.map { $0 }
  3. foo.map { return $0 }
  4. foo.map { elem in
  5. elem
  6. }
  7. foo.map { elem in
  8. return elem
  9. }
  10. foo.map { (elem: String) in
  11. elem
  12. }
  13. foo.map { elem -> String in
  14. elem
  15. }
  16. foo.map { $0 /* a comment */ }

Attributes

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
Attributes style 3.0.0

属性修饰符在修饰类型和函数时需要单独一行,但在修饰变量和引入时须写在同一行

示例

非触发

  1. @objc var x: String
  2. @objc private var x: String
  3. @nonobjc var x: String
  4. @IBOutlet private var label: UILabel
  5. @IBOutlet @objc private var label: UILabel
  6. @NSCopying var name: NSString
  7. @NSManaged var name: String?
  8. @IBInspectable var cornerRadius: CGFloat
  9. @available(iOS 9.0, *)
  10. let stackView: UIStackView
  11. @NSManaged func addSomeObject(book: SomeObject)
  12. @IBAction func buttonPressed(button: UIButton)
  13. @objc
  14. @IBAction func buttonPressed(button: UIButton)
  15. @available(iOS 9.0, *)
  16. func animate(view: UIStackView)
  17. @available(iOS 9.0, *, message="A message")
  18. func animate(view: UIStackView)
  19. @nonobjc
  20. final class X
  21. @available(iOS 9.0, *)
  22. class UIStackView
  23. @NSApplicationMain
  24. class AppDelegate: NSObject, NSApplicationDelegate
  25. @UIApplicationMain
  26. class AppDelegate: NSObject, UIApplicationDelegate
  27. @IBDesignable
  28. class MyCustomView: UIView
  29. @testable import SourceKittenFramework
  30. @objc(foo_x)
  31. var x: String
  32. @available(iOS 9.0, *)
  33. @objc(abc_stackView)
  34. let stackView: UIStackView
  35. @objc(abc_addSomeObject:)
  36. @NSManaged func addSomeObject(book: SomeObject)
  37. @objc(ABCThing)
  38. @available(iOS 9.0, *)
  39. class Thing
  40. class Foo: NSObject {
  41. override var description: String { return "" }
  42. }
  43. class Foo: NSObject {
  44. override func setUp() {}
  45. }
  46. @objc
  47. class {}
  48. extension Property {
  49. @available(*, unavailable, renamed: "isOptional")
  50. public var optional: Bool { fatalError() }
  51. }
  52. @GKInspectable var maxSpeed: Float
  53. @discardableResult
  54. func a() -> Int
  55. @objc
  56. @discardableResult
  57. func a() -> Int
  58. func increase(f: @autoclosure () -> Int) -> Int
  59. func foo(completionHandler: @escaping () -> Void)
  60. private struct DefaultError: Error {}
  61. @testable import foo
  62. private let bar = 1
  63. import XCTest
  64. @testable import DeleteMe
  65. @available (iOS 11.0, *)
  66. class DeleteMeTests: XCTestCase {
  67. }

触发

  1. @objc
  2. var x: String
  3. @objc
  4. var x: String
  5. @objc
  6. private var x: String
  7. @nonobjc
  8. var x: String
  9. @IBOutlet
  10. private var label: UILabel
  11. @IBOutlet
  12. private var label: UILabel
  13. @NSCopying
  14. var name: NSString
  15. @NSManaged
  16. var name: String?
  17. @IBInspectable
  18. var cornerRadius: CGFloat
  19. @available(iOS 9.0, *) let stackView: UIStackView
  20. @NSManaged
  21. func addSomeObject(book: SomeObject)
  22. @IBAction
  23. func buttonPressed(button: UIButton)
  24. @IBAction
  25. @objc
  26. func buttonPressed(button: UIButton)
  27. @available(iOS 9.0, *) func animate(view: UIStackView)
  28. @nonobjc final class X
  29. @available(iOS 9.0, *) class UIStackView
  30. @available(iOS 9.0, *)
  31. @objc class UIStackView
  32. @available(iOS 9.0, *) @objc
  33. class UIStackView
  34. @available(iOS 9.0, *)
  35. class UIStackView
  36. @UIApplicationMain class AppDelegate: NSObject, UIApplicationDelegate
  37. @IBDesignable class MyCustomView: UIView
  38. @testable
  39. import SourceKittenFramework
  40. @testable
  41. import SourceKittenFramework
  42. @objc(foo_x) var x: String
  43. @available(iOS 9.0, *) @objc(abc_stackView)
  44. let stackView: UIStackView
  45. @objc(abc_addSomeObject:) @NSManaged
  46. func addSomeObject(book: SomeObject)
  47. @objc(abc_addSomeObject:)
  48. @NSManaged
  49. func addSomeObject(book: SomeObject)
  50. @available(iOS 9.0, *)
  51. @objc(ABCThing) class Thing
  52. @GKInspectable
  53. var maxSpeed: Float
  54. @discardableResult func a() -> Int
  55. @objc
  56. @discardableResult func a() -> Int
  57. @objc
  58. @discardableResult
  59. func a() -> Int

Block Based KVO

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
block_based_kvo idiomatic 3.0.0

Swift3.2只后 最好使用block的方式来调用KVO的API

示例

非触发

  1. let observer = foo.observe(\.value, options: [.new]) { (foo, change) in
  2. print(change.newValue)
  3. }

触发

  1. class Foo: NSObject {
  2. override func observeValue(forKeyPath keyPath: String?, of object: Any?,
  3. change: [NSKeyValueChangeKey : Any]?,
  4. context: UnsafeMutableRawPointer?) {}
  5. }
  6. class Foo: NSObject {
  7. override func observeValue(forKeyPath keyPath: String?, of object: Any?,
  8. change: Dictionary<NSKeyValueChangeKey, Any>?,
  9. context: UnsafeMutableRawPointer?) {}
  10. }

Class Delegate Protocol

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
class_delegate_protocol lint 3.0.0

代理模式的delegate只能用于class,这样才可以被置弱引用。

示例

非触发

  1. protocol FooDelegate: class {}
  2. protocol FooDelegate: class, BarDelegate {}
  3. protocol Foo {}
  4. class FooDelegate {}
  5. @objc protocol FooDelegate {}
  6. @objc(MyFooDelegate)
  7. protocol FooDelegate {}
  8. protocol FooDelegate: BarDelegate {}
  9. protocol FooDelegate: AnyObject {}
  10. protocol FooDelegate: NSObjectProtocol {}

触发

  1. protocol FooDelegate {}
  2. protocol FooDelegate: Bar {}

Closing Brace Spacing

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
closing_brace style 3.0.0

闭合括号和内容之间不应该存在空格

示例

非触发

  1. [].map({ })
  2. [].map(
  3. { }
  4. )

触发

  1. [].map({ ↓} )
  2. [].map({ ↓} )

Closure Body Length

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
closure_body_length metrics 4.2.0

闭包不应该过长

示例

非触发

  1. foo.bar { $0 }
  2. foo.bar { toto in
  3. }
  4. foo.bar { toto in
  5. let a = 0
  6. // toto
  7. // toto
  8. // toto
  9. // toto
  10. // toto
  11. // toto
  12. // toto
  13. // toto
  14. // toto
  15. // toto
  16. }
  17. foo.bar { toto in
  18. let a = 0
  19. let a = 0
  20. let a = 0
  21. let a = 0
  22. let a = 0
  23. let a = 0
  24. let a = 0
  25. let a = 0
  26. let a = 0
  27. let a = 0
  28. let a = 0
  29. let a = 0
  30. let a = 0
  31. let a = 0
  32. let a = 0
  33. let a = 0
  34. let a = 0
  35. let a = 0
  36. let a = 0
  37. }
  38. foo.bar { toto in
  39. let a = 0
  40. let a = 0
  41. let a = 0
  42. let a = 0
  43. let a = 0
  44. let a = 0
  45. let a = 0
  46. let a = 0
  47. let a = 0
  48. let a = 0
  49. let a = 0
  50. let a = 0
  51. let a = 0
  52. let a = 0
  53. let a = 0
  54. let a = 0
  55. let a = 0
  56. let a = 0
  57. let a = 0
  58. // toto
  59. // toto
  60. // toto
  61. // toto
  62. // toto
  63. // toto
  64. // toto
  65. // toto
  66. // toto
  67. // toto
  68. }
  69. foo.bar({ toto in
  70. })
  71. foo.bar({ toto in
  72. let a = 0
  73. })
  74. foo.bar({ toto in
  75. let a = 0
  76. let a = 0
  77. let a = 0
  78. let a = 0
  79. let a = 0
  80. let a = 0
  81. let a = 0
  82. let a = 0
  83. let a = 0
  84. let a = 0
  85. let a = 0
  86. let a = 0
  87. let a = 0
  88. let a = 0
  89. let a = 0
  90. let a = 0
  91. let a = 0
  92. let a = 0
  93. let a = 0
  94. })
  95. foo.bar(label: { toto in
  96. })
  97. foo.bar(label: { toto in
  98. let a = 0
  99. })
  100. foo.bar(label: { toto in
  101. let a = 0
  102. let a = 0
  103. let a = 0
  104. let a = 0
  105. let a = 0
  106. let a = 0
  107. let a = 0
  108. let a = 0
  109. let a = 0
  110. let a = 0
  111. let a = 0
  112. let a = 0
  113. let a = 0
  114. let a = 0
  115. let a = 0
  116. let a = 0
  117. let a = 0
  118. let a = 0
  119. let a = 0
  120. })
  121. foo.bar(label: { toto in
  122. let a = 0
  123. let a = 0
  124. let a = 0
  125. let a = 0
  126. let a = 0
  127. let a = 0
  128. let a = 0
  129. let a = 0
  130. let a = 0
  131. let a = 0
  132. let a = 0
  133. let a = 0
  134. let a = 0
  135. let a = 0
  136. let a = 0
  137. let a = 0
  138. let a = 0
  139. let a = 0
  140. let a = 0
  141. }, anotherLabel: { toto in
  142. let a = 0
  143. let a = 0
  144. let a = 0
  145. let a = 0
  146. let a = 0
  147. let a = 0
  148. let a = 0
  149. let a = 0
  150. let a = 0
  151. let a = 0
  152. let a = 0
  153. let a = 0
  154. let a = 0
  155. let a = 0
  156. let a = 0
  157. let a = 0
  158. let a = 0
  159. let a = 0
  160. let a = 0
  161. })
  162. foo.bar(label: { toto in
  163. let a = 0
  164. let a = 0
  165. let a = 0
  166. let a = 0
  167. let a = 0
  168. let a = 0
  169. let a = 0
  170. let a = 0
  171. let a = 0
  172. let a = 0
  173. let a = 0
  174. let a = 0
  175. let a = 0
  176. let a = 0
  177. let a = 0
  178. let a = 0
  179. let a = 0
  180. let a = 0
  181. let a = 0
  182. }) { toto in
  183. let a = 0
  184. let a = 0
  185. let a = 0
  186. let a = 0
  187. let a = 0
  188. let a = 0
  189. let a = 0
  190. let a = 0
  191. let a = 0
  192. let a = 0
  193. let a = 0
  194. let a = 0
  195. let a = 0
  196. let a = 0
  197. let a = 0
  198. let a = 0
  199. let a = 0
  200. let a = 0
  201. let a = 0
  202. }
  203. let foo: Bar = { toto in
  204. let bar = Bar()
  205. let a = 0
  206. let a = 0
  207. let a = 0
  208. let a = 0
  209. let a = 0
  210. let a = 0
  211. let a = 0
  212. let a = 0
  213. let a = 0
  214. let a = 0
  215. let a = 0
  216. let a = 0
  217. let a = 0
  218. let a = 0
  219. let a = 0
  220. let a = 0
  221. let a = 0
  222. let a = 0
  223. return bar
  224. }()

触发

  1. foo.bar ↓{ toto in
  2. let a = 0
  3. let a = 0
  4. let a = 0
  5. let a = 0
  6. let a = 0
  7. let a = 0
  8. let a = 0
  9. let a = 0
  10. let a = 0
  11. let a = 0
  12. let a = 0
  13. let a = 0
  14. let a = 0
  15. let a = 0
  16. let a = 0
  17. let a = 0
  18. let a = 0
  19. let a = 0
  20. let a = 0
  21. let a = 0
  22. let a = 0
  23. }
  24. foo.bar ↓{ toto in
  25. let a = 0
  26. let a = 0
  27. let a = 0
  28. let a = 0
  29. let a = 0
  30. let a = 0
  31. let a = 0
  32. let a = 0
  33. let a = 0
  34. let a = 0
  35. let a = 0
  36. let a = 0
  37. let a = 0
  38. let a = 0
  39. let a = 0
  40. let a = 0
  41. let a = 0
  42. let a = 0
  43. let a = 0
  44. let a = 0
  45. let a = 0
  46. // toto
  47. // toto
  48. // toto
  49. // toto
  50. // toto
  51. // toto
  52. // toto
  53. // toto
  54. // toto
  55. // toto
  56. }
  57. foo.bar(↓{ toto in
  58. let a = 0
  59. let a = 0
  60. let a = 0
  61. let a = 0
  62. let a = 0
  63. let a = 0
  64. let a = 0
  65. let a = 0
  66. let a = 0
  67. let a = 0
  68. let a = 0
  69. let a = 0
  70. let a = 0
  71. let a = 0
  72. let a = 0
  73. let a = 0
  74. let a = 0
  75. let a = 0
  76. let a = 0
  77. let a = 0
  78. let a = 0
  79. })
  80. foo.bar(label: ↓{ toto in
  81. let a = 0
  82. let a = 0
  83. let a = 0
  84. let a = 0
  85. let a = 0
  86. let a = 0
  87. let a = 0
  88. let a = 0
  89. let a = 0
  90. let a = 0
  91. let a = 0
  92. let a = 0
  93. let a = 0
  94. let a = 0
  95. let a = 0
  96. let a = 0
  97. let a = 0
  98. let a = 0
  99. let a = 0
  100. let a = 0
  101. let a = 0
  102. })
  103. foo.bar(label: ↓{ toto in
  104. let a = 0
  105. let a = 0
  106. let a = 0
  107. let a = 0
  108. let a = 0
  109. let a = 0
  110. let a = 0
  111. let a = 0
  112. let a = 0
  113. let a = 0
  114. let a = 0
  115. let a = 0
  116. let a = 0
  117. let a = 0
  118. let a = 0
  119. let a = 0
  120. let a = 0
  121. let a = 0
  122. let a = 0
  123. let a = 0
  124. let a = 0
  125. }, anotherLabel: ↓{ toto in
  126. let a = 0
  127. let a = 0
  128. let a = 0
  129. let a = 0
  130. let a = 0
  131. let a = 0
  132. let a = 0
  133. let a = 0
  134. let a = 0
  135. let a = 0
  136. let a = 0
  137. let a = 0
  138. let a = 0
  139. let a = 0
  140. let a = 0
  141. let a = 0
  142. let a = 0
  143. let a = 0
  144. let a = 0
  145. let a = 0
  146. let a = 0
  147. })
  148. foo.bar(label: ↓{ toto in
  149. let a = 0
  150. let a = 0
  151. let a = 0
  152. let a = 0
  153. let a = 0
  154. let a = 0
  155. let a = 0
  156. let a = 0
  157. let a = 0
  158. let a = 0
  159. let a = 0
  160. let a = 0
  161. let a = 0
  162. let a = 0
  163. let a = 0
  164. let a = 0
  165. let a = 0
  166. let a = 0
  167. let a = 0
  168. let a = 0
  169. let a = 0
  170. }) ↓{ toto in
  171. let a = 0
  172. let a = 0
  173. let a = 0
  174. let a = 0
  175. let a = 0
  176. let a = 0
  177. let a = 0
  178. let a = 0
  179. let a = 0
  180. let a = 0
  181. let a = 0
  182. let a = 0
  183. let a = 0
  184. let a = 0
  185. let a = 0
  186. let a = 0
  187. let a = 0
  188. let a = 0
  189. let a = 0
  190. let a = 0
  191. let a = 0
  192. }
  193. let foo: Bar = ↓{ toto in
  194. let bar = Bar()
  195. let a = 0
  196. let a = 0
  197. let a = 0
  198. let a = 0
  199. let a = 0
  200. let a = 0
  201. let a = 0
  202. let a = 0
  203. let a = 0
  204. let a = 0
  205. let a = 0
  206. let a = 0
  207. let a = 0
  208. let a = 0
  209. let a = 0
  210. let a = 0
  211. let a = 0
  212. let a = 0
  213. let a = 0
  214. return bar
  215. }()

补充

感觉这个示例有点看不懂 为何非触发里边也可以写那么长。。。


Closure End Indentation

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
closure_end_indentation style 3.0.0

闭包的结尾应该和它的调用有同样的缩进

示例

非触发

  1. SignalProducer(values: [1, 2, 3])
  2. .startWithNext { number in
  3. print(number)
  4. }
  5. [1, 2].map { $0 + 1 }
  6. return match(pattern: pattern, with: [.comment]).flatMap { range in
  7. return Command(string: contents, range: range)
  8. }.flatMap { command in
  9. return command.expand()
  10. }
  11. foo(foo: bar,
  12. options: baz) { _ in }
  13. someReallyLongProperty.chainingWithAnotherProperty
  14. .foo { _ in }
  15. foo(abc, 123)
  16. { _ in }
  17. function(
  18. closure: { x in
  19. print(x)
  20. },
  21. anotherClosure: { y in
  22. print(y)
  23. })
  24. function(parameter: param,
  25. closure: { x in
  26. print(x)
  27. })
  28. function(parameter: param, closure: { x in
  29. print(x)
  30. },
  31. anotherClosure: { y in
  32. print(y)
  33. })

触发

  1. SignalProducer(values: [1, 2, 3])
  2. .startWithNext { number in
  3. print(number)
  4. ↓}
  5. return match(pattern: pattern, with: [.comment]).flatMap { range in
  6. return Command(string: contents, range: range)
  7. ↓}.flatMap { command in
  8. return command.expand()
  9. ↓}
  10. function(
  11. closure: { x in
  12. print(x)
  13. ↓},
  14. anotherClosure: { y in
  15. print(y)
  16. ↓})

Closure Parameter Position

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
closure_parameter_position style 3.0.0

闭包参数应该和左括号在同一行

示例

非触发

  1. [1, 2].map { $0 + 1 }
  2. [1, 2].map({ $0 + 1 })
  3. [1, 2].map { number in
  4. number + 1
  5. }
  6. [1, 2].map { number -> Int in
  7. number + 1
  8. }
  9. [1, 2].map { (number: Int) -> Int in
  10. number + 1
  11. }
  12. [1, 2].map { [weak self] number in
  13. number + 1
  14. }
  15. [1, 2].something(closure: { number in
  16. number + 1
  17. })
  18. let isEmpty = [1, 2].isEmpty()
  19. rlmConfiguration.migrationBlock.map { rlmMigration in
  20. return { migration, schemaVersion in
  21. rlmMigration(migration.rlmMigration, schemaVersion)
  22. }
  23. }
  24. let mediaView: UIView = { [weak self] index in
  25. return UIView()
  26. }(index)

触发

  1. [1, 2].map {
  2. number in
  3. number + 1
  4. }
  5. [1, 2].map {
  6. number -> Int in
  7. number + 1
  8. }
  9. [1, 2].map {
  10. (↓number: Int) -> Int in
  11. number + 1
  12. }
  13. [1, 2].map {
  14. [weak self] number in
  15. number + 1
  16. }
  17. [1, 2].map { [weak self]
  18. number in
  19. number + 1
  20. }
  21. [1, 2].map({
  22. number in
  23. number + 1
  24. })
  25. [1, 2].something(closure: {
  26. number in
  27. number + 1
  28. })
  29. [1, 2].reduce(0) {
  30. sum, number in
  31. number + sum
  32. }

Closure Spacing

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
closure_spacing style 3.0.0

闭包表达式应该与前后括号之间有一个空格

示例

非触发

  1. [].map ({ $0.description })
  2. [].filter { $0.contains(location) }
  3. extension UITableViewCell: ReusableView { }
  4. extension UITableViewCell: ReusableView {}

触发

  1. [].filter(↓{$0.contains(location)})
  2. [].map(↓{$0})
  3. (↓{each in return result.contains(where: ↓{e in return e}) }).count
  4. filter ↓{ sorted ↓{ $0 < $1}}

Collection Element Alignment

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
collection_alignment style 3.0.0

集合中的所有元素都应该垂直对齐。

示例

非触发

  1. doThings(arg: [
  2. "foo": 1,
  3. "bar": 2,
  4. "fizz": 2,
  5. "buzz": 2
  6. ])
  7. let abc = [
  8. "alpha": "a",
  9. "beta": "b",
  10. "gamma": "g",
  11. "delta": "d",
  12. "epsilon": "e"
  13. ]
  14. let meals = [
  15. "breakfast": "oatmeal",
  16. "lunch": "sandwich",
  17. "dinner": "burger"
  18. ]
  19. let coordinates = [
  20. CLLocationCoordinate2D(latitude: 0, longitude: 33),
  21. CLLocationCoordinate2D(latitude: 0, longitude: 66),
  22. CLLocationCoordinate2D(latitude: 0, longitude: 99)
  23. ]
  24. var evenNumbers: Set<Int> = [
  25. 2,
  26. 4,
  27. 6
  28. ]
  29. let abc = [1, 2, 3, 4]
  30. let abc = [
  31. 1, 2, 3, 4
  32. ]
  33. let abc = [
  34. "foo": "bar", "fizz": "buzz"
  35. ]
  36. var evenNumbers: Set<Int> = [
  37. 2,
  38. 4,
  39. 6
  40. ]
  41. let abc = [1, 2, 3, 4]
  42. let abc = [
  43. 1, 2, 3, 4
  44. ]
  45. let abc = [
  46. "foo": "bar", "fizz": "buzz"
  47. ]

触发

  1. doThings(arg: [
  2. "foo": 1,
  3. "bar": 2,
  4. "fizz": 2,
  5. "buzz": 2
  6. ])
  7. let abc = [
  8. "alpha": "a",
  9. "beta": "b",
  10. "gamma": "g",
  11. "delta": "d",
  12. "epsilon": "e"
  13. ]
  14. let meals = [
  15. "breakfast": "oatmeal",
  16. "lunch": "sandwich",
  17. "dinner": "burger"
  18. ]
  19. let coordinates = [
  20. CLLocationCoordinate2D(latitude: 0, longitude: 33),
  21. CLLocationCoordinate2D(latitude: 0, longitude: 66),
  22. CLLocationCoordinate2D(latitude: 0, longitude: 99)
  23. ]
  24. var evenNumbers: Set<Int> = [
  25. 2,
  26. 4,
  27. 6
  28. ]

Colon

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
colon style 3.0.0

声明类型时,冒号应该紧邻标识符;字典中应与key紧邻。

示例

非触发

  1. let abc: Void
  2. let abc: [Void: Void]
  3. let abc: (Void, Void)
  4. let abc: ([Void], String, Int)
  5. let abc: [([Void], String, Int)]
  6. let abc: String="def"
  7. let abc: Int=0
  8. let abc: Enum=Enum.Value
  9. func abc(def: Void) {}
  10. func abc(def: Void, ghi: Void) {}
  11. let abc: String = "abc:"
  12. let abc = [Void: Void]()
  13. let abc = [1: [3: 2], 3: 4]
  14. let abc = ["string": "string"]
  15. let abc = ["string:string": "string"]
  16. let abc: [String: Int]
  17. func foo(bar: [String: Int]) {}
  18. func foo() -> [String: Int] { return [:] }
  19. let abc: Any
  20. let abc: [Any: Int]
  21. let abc: [String: Any]
  22. class Foo: Bar {}
  23. class Foo<T: Equatable> {}
  24. switch foo {
  25. case .bar:
  26. _ = something()
  27. }
  28. object.method(x: 5, y: "string")
  29. object.method(x: 5, y:
  30. "string")
  31. object.method(5, y: "string")
  32. func abc() { def(ghi: jkl) }
  33. func abc(def: Void) { ghi(jkl: mno) }
  34. class ABC { let def = ghi(jkl: mno) } }
  35. func foo() { let dict = [1: 1] }

触发

  1. let abc:Void
  2. let abc: Void
  3. let abc :Void
  4. let abc : Void
  5. let abc : [Void: Void]
  6. let abc : (Void, String, Int)
  7. let abc : ([Void], String, Int)
  8. let abc : [([Void], String, Int)]
  9. let abc: (Void, String, Int)
  10. let abc: ([Void], String, Int)
  11. let abc: [([Void], String, Int)]
  12. let abc :String="def"
  13. let abc :Int=0
  14. let abc :Int = 0
  15. let abc:Int=0
  16. let abc:Int = 0
  17. let abc:Enum=Enum.Value
  18. func abc(↓def:Void) {}
  19. func abc(↓def: Void) {}
  20. func abc(↓def :Void) {}
  21. func abc(↓def : Void) {}
  22. func abc(def: Void, ghi :Void) {}
  23. let abc = [Void↓:Void]()
  24. let abc = [Void : Void]()
  25. let abc = [Void↓: Void]()
  26. let abc = [Void : Void]()
  27. let abc = [1: [3 : 2], 3: 4]
  28. let abc = [1: [3 : 2], 3↓: 4]
  29. let abc: [↓String : Int]
  30. let abc: [↓String:Int]
  31. func foo(bar: [↓String : Int]) {}
  32. func foo(bar: [↓String:Int]) {}
  33. func foo() -> [↓String : Int] { return [:] }
  34. func foo() -> [↓String:Int] { return [:] }
  35. let abc : Any
  36. let abc: [↓Any : Int]
  37. let abc: [↓String : Any]
  38. class Foo : Bar {}
  39. class Foo:Bar {}
  40. class Foo<↓T:Equatable> {}
  41. class Foo<↓T : Equatable> {}
  42. object.method(x: 5, y : "string")
  43. object.method(x↓:5, y: "string")
  44. object.method(x↓: 5, y: "string")
  45. func abc() { def(ghi↓:jkl) }
  46. func abc(def: Void) { ghi(jkl↓:mno) }
  47. class ABC { let def = ghi(jkl↓:mno) } }
  48. func foo() { let dict = [1 : 1] }

Comma Spacing

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
comma style 3.0.0

任何逗号前边都不应该有空格。

示例

非触发

  1. func abc(a: String, b: String) { }
  2. abc(a: "string", b: "string"
  3. enum a { case a, b, c }
  4. func abc(
  5. a: String, // comment
  6. bcd: String // comment
  7. ) {
  8. }
  9. func abc(
  10. a: String,
  11. bcd: String
  12. ) {
  13. }
  14. #imageLiteral(resourceName: "foo,bar,baz")

触发

  1. func abc(a: String ,b: String) { }
  2. func abc(a: String ,b: String ,c: String ,d: String) { }
  3. abc(a: "string"↓,b: "string"
  4. enum a { case a ,b }
  5. let result = plus(
  6. first: 3 , // #683
  7. second: 4
  8. )

Compiler Protocol Init

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
compiler_protocol_init lint 3.0.0

遵循例如ExpressibleByArrayLiteral协议的初始化方法不应当被调用

示例

非触发

  1. let set: Set<Int> = [1, 2]
  2. let set = Set(array)

触发

  1. let set = Set(arrayLiteral: 1, 2)
  2. let set = Set.init(arrayLiteral: 1, 2)

补充

这一点苹果的文档已经加以说明了 该方法是给编译器使用的,我们初始化应该采用逗号的这种形式。

  1. /// Do not call this initializer directly. It is used by the compiler when
  2. /// you use an array literal. Instead, create a new set using an array
  3. /// literal as its value by enclosing a comma ...
  4. public init(arrayLiteral elements: Set.Element...)

Conditional Returns on Newline

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
conditional_returns_on_newline style 3.0.0

条件表达式的返回值需要另起一行。

示例

非触发

  1. guard true else {
  2. return true
  3. }
  4. guard true,
  5. let x = true else {
  6. return true
  7. }
  8. if true else {
  9. return true
  10. }
  11. if true,
  12. let x = true else {
  13. return true
  14. }
  15. if textField.returnKeyType == .Next {
  16. if true { // return }
  17. /*if true { */ return }

触发

  1. guard true else { return }
  2. if true { return }
  3. if true { break } else { return }
  4. if true { break } else { return }
  5. if true { return "YES" } else { return "NO" }

Contains over first not nil

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
contains_over_first_not_nil performance 3.0.0

contains优于first(where:) != nil

示例

非触发

  1. let first = myList.first(where: { $0 % 2 == 0 })
  2. let first = myList.first { $0 % 2 == 0 }

触发

  1. myList.first { $0 % 2 == 0 } != nil
  2. myList.first(where: { $0 % 2 == 0 }) != nil
  3. myList.map { $0 + 1 }.first(where: { $0 % 2 == 0 }) != nil
  4. myList.first(where: someFunction) != nil
  5. myList.map { $0 + 1 }.first { $0 % 2 == 0 } != nil
  6. (↓myList.first { $0 % 2 == 0 }) != nil

Control Statement

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
control_statement style 3.0.0

if, for, guard, switch, while, catch关键字的条件、参数没必要用括号括起来。

示例

非触发

  1. if condition {
  2. if (a, b) == (0, 1) {
  3. if (a || b) && (c || d) {
  4. if (min...max).contains(value) {
  5. if renderGif(data) {
  6. renderGif(data)
  7. for item in collection {
  8. for (key, value) in dictionary {
  9. for (index, value) in enumerate(array) {
  10. for var index = 0; index < 42; index++ {
  11. guard condition else {
  12. while condition {
  13. } while condition {
  14. do { ; } while condition {
  15. switch foo {
  16. do {
  17. } catch let error as NSError {
  18. }
  19. foo().catch(all: true) {}
  20. if max(a, b) < c {
  21. switch (lhs, rhs) {

触发

  1. if (condition) {
  2. if(condition) {
  3. if (condition == endIndex) {
  4. if ((a || b) && (c || d)) {
  5. if ((min...max).contains(value)) {
  6. for (item in collection) {
  7. for (var index = 0; index < 42; index++) {
  8. for(item in collection) {
  9. for(var index = 0; index < 42; index++) {
  10. guard (condition) else {
  11. while (condition) {
  12. while(condition) {
  13. } while (condition) {
  14. } while(condition) {
  15. do { ; } while(condition) {
  16. do { ; } while (condition) {
  17. switch (foo) {
  18. do {
  19. } catch(let error as NSError) {
  20. }
  21. if (max(a, b) < c) {

Convenience Type

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
convenience_type idiomatic 4.1.0

只用于存储静态成员的类型应该使用无壳的enum以避免初始化。

示例

非触发

  1. enum Math { // enum
  2. public static let pi = 3.14
  3. }
  4. // class with inheritance
  5. class MathViewController: UIViewController {
  6. public static let pi = 3.14
  7. }
  8. @objc class Math: NSObject { // class visible to Obj-C
  9. public static let pi = 3.14
  10. }
  11. struct Math { // type with non-static declarations
  12. public static let pi = 3.14
  13. public let randomNumber = 2
  14. }
  15. class DummyClass {}

触发

  1. struct Math {
  2. public static let pi = 3.14
  3. }
  4. class Math {
  5. public static let pi = 3.14
  6. }
  7. struct Math {
  8. public static let pi = 3.14
  9. @available(*, unavailable) init() {}
  10. }

Custom Rules

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
custom_rules Style No 3.0.0

通过正则表达式来创建自定义规则。
(可选)指定要匹配的语法类型,严重程度和提示信息。


Cyclomatic Complexity

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
cyclomatic_complexity metrics 3.0.0

应该控制函数的复杂度。

示例

非触发

  1. func f1() {
  2. if true {
  3. for _ in 1..5 { } }
  4. if false { }
  5. }
  6. func f(code: Int) -> Int {switch code {
  7. case 0: fallthrough
  8. case 0: return 1
  9. case 0: return 1
  10. case 0: return 1
  11. case 0: return 1
  12. case 0: return 1
  13. case 0: return 1
  14. case 0: return 1
  15. case 0: return 1
  16. default: return 1}}
  17. func f1() {if true {}; if true {}; if true {}; if true {}; if true {}; if true {}
  18. func f2() {
  19. if true {}; if true {}; if true {}; if true {}; if true {}
  20. }}

触发

  1. func f1() {
  2. if true {
  3. if true {
  4. if false {}
  5. }
  6. }
  7. if false {}
  8. let i = 0
  9. switch i {
  10. case 1: break
  11. case 2: break
  12. case 3: break
  13. case 4: break
  14. default: break
  15. }
  16. for _ in 1...5 {
  17. guard true else {
  18. return
  19. }
  20. }
  21. }

Discarded Notification Center Observer

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
discarded_notification_center_observer lint 3.0.0

当使用block注册观察者时, 不可见的观察者应当被返回并存储,以便后续移除。

示例

非触发

  1. let foo = nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil) { }
  2. let foo = nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { })
  3. func foo() -> Any {
  4. return nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { })
  5. }

触发

  1. nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil) { }
  2. nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { })
  3. @discardableResult func foo() -> Any {
  4. return nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { })
  5. }

Discouraged Direct Initialization

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
discouraged_direact_initialization lint 3.0.0

不推荐直接初始化有副作用的类型。

示例

非触发

  1. let foo = UIDevice.current
  2. let foo = Bundle.main
  3. let foo = Bundle(path: "bar")
  4. let foo = Bundle(identifier: "bar")
  5. let foo = Bundle.init(path: "bar")
  6. let foo = Bundle.init(identifier: "bar")

触发

  1. UIDevice()
  2. Bundle()
  3. let foo = UIDevice()
  4. let foo = Bundle()
  5. let foo = bar(bundle: Bundle(), device: UIDevice())
  6. UIDevice.init()
  7. Bundle.init()
  8. let foo = UIDevice.init()
  9. let foo = Bundle.init()
  10. let foo = bar(bundle: Bundle.init(), device: UIDevice.init())

Discouraged Object Literal

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
discouraged_object_literal idiomatic 3.0.0

首选初始化赋值,不推荐使用Object Literal。

示例

非触发

  1. let image = UIImage(named: aVariable)
  2. let image = UIImage(named: "interpolated \(variable)")
  3. let color = UIColor(red: value, green: value, blue: value, alpha: 1)
  4. let image = NSImage(named: aVariable)
  5. let image = NSImage(named: "interpolated \(variable)")
  6. let color = NSColor(red: value, green: value, blue: value, alpha: 1)

触发

  1. let image = ↓#imageLiteral(resourceName: "image.jpg")
  2. let color = ↓#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)

补充

swift-literal


Discouraged Optional Boolean

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
discouraged_optional_boolean idiomatic 3.0.0

首选使用非可选的布尔类型。

示例

非触发

  1. var foo: Bool
  2. var foo: [String: Bool]
  3. var foo: [Bool]
  4. let foo: Bool = true
  5. let foo: Bool = false
  6. let foo: [String: Bool] = [:]
  7. let foo: [Bool] = []
  8. var foo: Bool { return true }
  9. let foo: Bool { return false }()
  10. func foo() -> Bool {}
  11. func foo() -> [String: Bool] {}
  12. func foo() -> ([Bool]) -> String {}
  13. func foo(input: Bool = true) {}
  14. func foo(input: [String: Bool] = [:]) {}
  15. func foo(input: [Bool] = []) {}
  16. class Foo {
  17. func foo() -> Bool {}
  18. }
  19. class Foo {
  20. func foo() -> [String: Bool] {}
  21. }
  22. class Foo {
  23. func foo() -> ([Bool]) -> String {}
  24. }
  25. struct Foo {
  26. func foo() -> Bool {}
  27. }
  28. struct Foo {
  29. func foo() -> [String: Bool] {}
  30. }
  31. struct Foo {
  32. func foo() -> ([Bool]) -> String {}
  33. }
  34. enum Foo {
  35. func foo() -> Bool {}
  36. }
  37. enum Foo {
  38. func foo() -> [String: Bool] {}
  39. }
  40. enum Foo {
  41. func foo() -> ([Bool]) -> String {}
  42. }
  43. class Foo {
  44. func foo(input: Bool = true) {}
  45. }
  46. class Foo {
  47. func foo(input: [String: Bool] = [:]) {}
  48. }
  49. class Foo {
  50. func foo(input: [Bool] = []) {}
  51. }
  52. struct Foo {
  53. func foo(input: Bool = true) {}
  54. }
  55. struct Foo {
  56. func foo(input: [String: Bool] = [:]) {}
  57. }
  58. struct Foo {
  59. func foo(input: [Bool] = []) {}
  60. }
  61. enum Foo {
  62. func foo(input: Bool = true) {}
  63. }
  64. enum Foo {
  65. func foo(input: [String: Bool] = [:]) {}
  66. }
  67. enum Foo {
  68. func foo(input: [Bool] = []) {}
  69. }

触发

  1. var foo: Bool?
  2. var foo: [String: Bool?]
  3. var foo: [↓Bool?]
  4. let foo: Bool? = nil
  5. let foo: [String: Bool?] = [:]
  6. let foo: [↓Bool?] = []
  7. let foo = Optional.some(false)
  8. let foo = Optional.some(true)
  9. var foo: Bool? { return nil }
  10. let foo: Bool? { return nil }()
  11. func foo() -> Bool? {}
  12. func foo() -> [String: Bool?] {}
  13. func foo() -> [↓Bool?] {}
  14. static func foo() -> Bool? {}
  15. static func foo() -> [String: Bool?] {}
  16. static func foo() -> [↓Bool?] {}
  17. func foo() -> (↓Bool?) -> String {}
  18. func foo() -> ([Int]) -> Bool? {}
  19. func foo(input: Bool?) {}
  20. func foo(input: [String: Bool?]) {}
  21. func foo(input: [↓Bool?]) {}
  22. static func foo(input: Bool?) {}
  23. static func foo(input: [String: Bool?]) {}
  24. static func foo(input: [↓Bool?]) {}
  25. class Foo {
  26. var foo: Bool?
  27. }
  28. class Foo {
  29. var foo: [String: Bool?]
  30. }
  31. class Foo {
  32. let foo: Bool? = nil
  33. }
  34. class Foo {
  35. let foo: [String: Bool?] = [:]
  36. }
  37. class Foo {
  38. let foo: [↓Bool?] = []
  39. }
  40. struct Foo {
  41. var foo: Bool?
  42. }
  43. struct Foo {
  44. var foo: [String: Bool?]
  45. }
  46. struct Foo {
  47. let foo: Bool? = nil
  48. }
  49. struct Foo {
  50. let foo: [String: Bool?] = [:]
  51. }
  52. struct Foo {
  53. let foo: [↓Bool?] = []
  54. }
  55. class Foo {
  56. var foo: Bool? { return nil }
  57. }
  58. class Foo {
  59. let foo: Bool? { return nil }()
  60. }
  61. struct Foo {
  62. var foo: Bool? { return nil }
  63. }
  64. struct Foo {
  65. let foo: Bool? { return nil }()
  66. }
  67. enum Foo {
  68. var foo: Bool? { return nil }
  69. }
  70. enum Foo {
  71. let foo: Bool? { return nil }()
  72. }
  73. class Foo {
  74. func foo() -> Bool? {}
  75. }
  76. class Foo {
  77. func foo() -> [String: Bool?] {}
  78. }
  79. class Foo {
  80. func foo() -> [↓Bool?] {}
  81. }
  82. class Foo {
  83. static func foo() -> Bool? {}
  84. }
  85. class Foo {
  86. static func foo() -> [String: Bool?] {}
  87. }
  88. class Foo {
  89. static func foo() -> [↓Bool?] {}
  90. }
  91. class Foo {
  92. func foo() -> (↓Bool?) -> String {}
  93. }
  94. class Foo {
  95. func foo() -> ([Int]) -> Bool? {}
  96. }
  97. struct Foo {
  98. func foo() -> Bool? {}
  99. }
  100. struct Foo {
  101. func foo() -> [String: Bool?] {}
  102. }
  103. struct Foo {
  104. func foo() -> [↓Bool?] {}
  105. }
  106. struct Foo {
  107. static func foo() -> Bool? {}
  108. }
  109. struct Foo {
  110. static func foo() -> [String: Bool?] {}
  111. }
  112. struct Foo {
  113. static func foo() -> [↓Bool?] {}
  114. }
  115. struct Foo {
  116. func foo() -> (↓Bool?) -> String {}
  117. }
  118. struct Foo {
  119. func foo() -> ([Int]) -> Bool? {}
  120. }
  121. enum Foo {
  122. func foo() -> Bool? {}
  123. }
  124. enum Foo {
  125. func foo() -> [String: Bool?] {}
  126. }
  127. enum Foo {
  128. func foo() -> [↓Bool?] {}
  129. }
  130. enum Foo {
  131. static func foo() -> Bool? {}
  132. }
  133. enum Foo {
  134. static func foo() -> [String: Bool?] {}
  135. }
  136. enum Foo {
  137. static func foo() -> [↓Bool?] {}
  138. }
  139. enum Foo {
  140. func foo() -> (↓Bool?) -> String {}
  141. }
  142. enum Foo {
  143. func foo() -> ([Int]) -> Bool? {}
  144. }
  145. class Foo {
  146. func foo(input: Bool?) {}
  147. }
  148. class Foo {
  149. func foo(input: [String: Bool?]) {}
  150. }
  151. class Foo {
  152. func foo(input: [↓Bool?]) {}
  153. }
  154. class Foo {
  155. static func foo(input: Bool?) {}
  156. }
  157. class Foo {
  158. static func foo(input: [String: Bool?]) {}
  159. }
  160. class Foo {
  161. static func foo(input: [↓Bool?]) {}
  162. }
  163. struct Foo {
  164. func foo(input: Bool?) {}
  165. }
  166. struct Foo {
  167. func foo(input: [String: Bool?]) {}
  168. }
  169. struct Foo {
  170. func foo(input: [↓Bool?]) {}
  171. }
  172. struct Foo {
  173. static func foo(input: Bool?) {}
  174. }
  175. struct Foo {
  176. static func foo(input: [String: Bool?]) {}
  177. }
  178. struct Foo {
  179. static func foo(input: [↓Bool?]) {}
  180. }
  181. enum Foo {
  182. func foo(input: Bool?) {}
  183. }
  184. enum Foo {
  185. func foo(input: [String: Bool?]) {}
  186. }
  187. enum Foo {
  188. func foo(input: [↓Bool?]) {}
  189. }
  190. enum Foo {
  191. static func foo(input: Bool?) {}
  192. }
  193. enum Foo {
  194. static func foo(input: [String: Bool?]) {}
  195. }
  196. enum Foo {
  197. static func foo(input: [↓Bool?]) {}
  198. }

Discouraged Optional Collection

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
discouraged_optional_collection lint 3.0.0

首选使用非可选的Collection

示例

非触发

  1. var foo: [Int]
  2. var foo: [String: Int]
  3. var foo: Set<String>
  4. var foo: [String: [String: Int]]
  5. let foo: [Int] = []
  6. let foo: [String: Int] = [:]
  7. let foo: Set<String> = []
  8. let foo: [String: [String: Int]] = [:]
  9. var foo: [Int] { return [] }
  10. func foo() -> [Int] {}
  11. func foo() -> [String: String] {}
  12. func foo() -> Set<Int> {}
  13. func foo() -> ([Int]) -> String {}
  14. func foo(input: [String] = []) {}
  15. func foo(input: [String: String] = [:]) {}
  16. func foo(input: Set<String> = []) {}
  17. class Foo {
  18. func foo() -> [Int] {}
  19. }
  20. class Foo {
  21. func foo() -> [String: String] {}
  22. }
  23. class Foo {
  24. func foo() -> Set<Int> {}
  25. }
  26. class Foo {
  27. func foo() -> ([Int]) -> String {}
  28. }
  29. struct Foo {
  30. func foo() -> [Int] {}
  31. }
  32. struct Foo {
  33. func foo() -> [String: String] {}
  34. }
  35. struct Foo {
  36. func foo() -> Set<Int> {}
  37. }
  38. struct Foo {
  39. func foo() -> ([Int]) -> String {}
  40. }
  41. enum Foo {
  42. func foo() -> [Int] {}
  43. }
  44. enum Foo {
  45. func foo() -> [String: String] {}
  46. }
  47. enum Foo {
  48. func foo() -> Set<Int> {}
  49. }
  50. enum Foo {
  51. func foo() -> ([Int]) -> String {}
  52. }
  53. class Foo {
  54. func foo(input: [String] = []) {}
  55. }
  56. class Foo {
  57. func foo(input: [String: String] = [:]) {}
  58. }
  59. class Foo {
  60. func foo(input: Set<String> = []) {}
  61. }
  62. struct Foo {
  63. func foo(input: [String] = []) {}
  64. }
  65. struct Foo {
  66. func foo(input: [String: String] = [:]) {}
  67. }
  68. struct Foo {
  69. func foo(input: Set<String> = []) {}
  70. }
  71. enum Foo {
  72. func foo(input: [String] = []) {}
  73. }
  74. enum Foo {
  75. func foo(input: [String: String] = [:]) {}
  76. }
  77. enum Foo {
  78. func foo(input: Set<String> = []) {}
  79. }

触发

  1. var foo: [Int]?
  2. var foo: [String: Int]?
  3. var foo: Set<String>?
  4. let foo: [Int]? = nil
  5. let foo: [String: Int]? = nil
  6. let foo: Set<String>? = nil
  7. var foo: [Int]? { return nil }
  8. let foo: [Int]? { return nil }()
  9. func foo() -> [T]? {}
  10. func foo() -> [String: String]? {}
  11. func foo() -> [String: [String: String]]? {}
  12. func foo() -> [String: [String: String]?] {}
  13. func foo() -> Set<Int>? {}
  14. static func foo() -> [T]? {}
  15. static func foo() -> [String: String]? {}
  16. static func foo() -> [String: [String: String]]? {}
  17. static func foo() -> [String: [String: String]?] {}
  18. static func foo() -> Set<Int>? {}
  19. func foo() -> ([Int]?) -> String {}
  20. func foo() -> ([Int]) -> [String]? {}
  21. func foo(↓input: [String: String]?) {}
  22. func foo(↓input: [String: [String: String]]?) {}
  23. func foo(↓input: [String: [String: String]?]) {}
  24. func foo(↓↓input: [String: [String: String]?]?) {}
  25. func foo<K, V>(_ dict1: [K: V], _ dict2: [K: V]?) -> [K: V]
  26. func foo<K, V>(dict1: [K: V], dict2: [K: V]?) -> [K: V]
  27. static func foo(↓input: [String: String]?) {}
  28. static func foo(↓input: [String: [String: String]]?) {}
  29. static func foo(↓input: [String: [String: String]?]) {}
  30. static func foo(↓↓input: [String: [String: String]?]?) {}
  31. static func foo<K, V>(_ dict1: [K: V], _ dict2: [K: V]?) -> [K: V]
  32. static func foo<K, V>(dict1: [K: V], dict2: [K: V]?) -> [K: V]
  33. class Foo {
  34. var foo: [Int]?
  35. }
  36. class Foo {
  37. var foo: [String: Int]?
  38. }
  39. class Foo {
  40. var foo: Set<String>?
  41. }
  42. class Foo {
  43. let foo: [Int]? = nil
  44. }
  45. class Foo {
  46. let foo: [String: Int]? = nil
  47. }
  48. class Foo {
  49. let foo: Set<String>? = nil
  50. }
  51. struct Foo {
  52. var foo: [Int]?
  53. }
  54. struct Foo {
  55. var foo: [String: Int]?
  56. }
  57. struct Foo {
  58. var foo: Set<String>?
  59. }
  60. struct Foo {
  61. let foo: [Int]? = nil
  62. }
  63. struct Foo {
  64. let foo: [String: Int]? = nil
  65. }
  66. struct Foo {
  67. let foo: Set<String>? = nil
  68. }
  69. class Foo {
  70. var foo: [Int]? { return nil }
  71. }
  72. class Foo {
  73. let foo: [Int]? { return nil }()
  74. }
  75. class Foo {
  76. var foo: Set<String>? { return nil }
  77. }
  78. class Foo {
  79. let foo: Set<String>? { return nil }()
  80. }
  81. struct Foo {
  82. var foo: [Int]? { return nil }
  83. }
  84. struct Foo {
  85. let foo: [Int]? { return nil }()
  86. }
  87. struct Foo {
  88. var foo: Set<String>? { return nil }
  89. }
  90. struct Foo {
  91. let foo: Set<String>? { return nil }()
  92. }
  93. enum Foo {
  94. var foo: [Int]? { return nil }
  95. }
  96. enum Foo {
  97. let foo: [Int]? { return nil }()
  98. }
  99. enum Foo {
  100. var foo: Set<String>? { return nil }
  101. }
  102. enum Foo {
  103. let foo: Set<String>? { return nil }()
  104. }
  105. class Foo {
  106. func foo() -> [T]? {}
  107. }
  108. class Foo {
  109. func foo() -> [String: String]? {}
  110. }
  111. class Foo {
  112. func foo() -> [String: [String: String]]? {}
  113. }
  114. class Foo {
  115. func foo() -> [String: [String: String]?] {}
  116. }
  117. class Foo {
  118. func foo() -> Set<Int>? {}
  119. }
  120. class Foo {
  121. static func foo() -> [T]? {}
  122. }
  123. class Foo {
  124. static func foo() -> [String: String]? {}
  125. }
  126. class Foo {
  127. static func foo() -> [String: [String: String]]? {}
  128. }
  129. class Foo {
  130. static func foo() -> [String: [String: String]?] {}
  131. }
  132. class Foo {
  133. static func foo() -> Set<Int>? {}
  134. }
  135. class Foo {
  136. func foo() -> ([Int]?) -> String {}
  137. }
  138. class Foo {
  139. func foo() -> ([Int]) -> [String]? {}
  140. }
  141. struct Foo {
  142. func foo() -> [T]? {}
  143. }
  144. struct Foo {
  145. func foo() -> [String: String]? {}
  146. }
  147. struct Foo {
  148. func foo() -> [String: [String: String]]? {}
  149. }
  150. struct Foo {
  151. func foo() -> [String: [String: String]?] {}
  152. }
  153. struct Foo {
  154. func foo() -> Set<Int>? {}
  155. }
  156. struct Foo {
  157. static func foo() -> [T]? {}
  158. }
  159. struct Foo {
  160. static func foo() -> [String: String]? {}
  161. }
  162. struct Foo {
  163. static func foo() -> [String: [String: String]]? {}
  164. }
  165. struct Foo {
  166. static func foo() -> [String: [String: String]?] {}
  167. }
  168. struct Foo {
  169. static func foo() -> Set<Int>? {}
  170. }
  171. struct Foo {
  172. func foo() -> ([Int]?) -> String {}
  173. }
  174. struct Foo {
  175. func foo() -> ([Int]) -> [String]? {}
  176. }
  177. enum Foo {
  178. func foo() -> [T]? {}
  179. }
  180. enum Foo {
  181. func foo() -> [String: String]? {}
  182. }
  183. enum Foo {
  184. func foo() -> [String: [String: String]]? {}
  185. }
  186. enum Foo {
  187. func foo() -> [String: [String: String]?] {}
  188. }
  189. enum Foo {
  190. func foo() -> Set<Int>? {}
  191. }
  192. enum Foo {
  193. static func foo() -> [T]? {}
  194. }
  195. enum Foo {
  196. static func foo() -> [String: String]? {}
  197. }
  198. enum Foo {
  199. static func foo() -> [String: [String: String]]? {}
  200. }
  201. enum Foo {
  202. static func foo() -> [String: [String: String]?] {}
  203. }
  204. enum Foo {
  205. static func foo() -> Set<Int>? {}
  206. }
  207. enum Foo {
  208. func foo() -> ([Int]?) -> String {}
  209. }
  210. enum Foo {
  211. func foo() -> ([Int]) -> [String]? {}
  212. }
  213. class Foo {
  214. func foo(↓input: [String: String]?) {}
  215. }
  216. class Foo {
  217. func foo(↓input: [String: [String: String]]?) {}
  218. }
  219. class Foo {
  220. func foo(↓input: [String: [String: String]?]) {}
  221. }
  222. class Foo {
  223. func foo(↓↓input: [String: [String: String]?]?) {}
  224. }
  225. class Foo {
  226. func foo<K, V>(_ dict1: [K: V], _ dict2: [K: V]?) -> [K: V]
  227. }
  228. class Foo {
  229. func foo<K, V>(dict1: [K: V], dict2: [K: V]?) -> [K: V]
  230. }
  231. class Foo {
  232. static func foo(↓input: [String: String]?) {}
  233. }
  234. class Foo {
  235. static func foo(↓input: [String: [String: String]]?) {}
  236. }
  237. class Foo {
  238. static func foo(↓input: [String: [String: String]?]) {}
  239. }
  240. class Foo {
  241. static func foo(↓↓input: [String: [String: String]?]?) {}
  242. }
  243. class Foo {
  244. static func foo<K, V>(_ dict1: [K: V], _ dict2: [K: V]?) -> [K: V]
  245. }
  246. class Foo {
  247. static func foo<K, V>(dict1: [K: V], dict2: [K: V]?) -> [K: V]
  248. }
  249. struct Foo {
  250. func foo(↓input: [String: String]?) {}
  251. }
  252. struct Foo {
  253. func foo(↓input: [String: [String: String]]?) {}
  254. }
  255. struct Foo {
  256. func foo(↓input: [String: [String: String]?]) {}
  257. }
  258. struct Foo {
  259. func foo(↓↓input: [String: [String: String]?]?) {}
  260. }
  261. struct Foo {
  262. func foo<K, V>(_ dict1: [K: V], _ dict2: [K: V]?) -> [K: V]
  263. }
  264. struct Foo {
  265. func foo<K, V>(dict1: [K: V], dict2: [K: V]?) -> [K: V]
  266. }
  267. struct Foo {
  268. static func foo(↓input: [String: String]?) {}
  269. }
  270. struct Foo {
  271. static func foo(↓input: [String: [String: String]]?) {}
  272. }
  273. struct Foo {
  274. static func foo(↓input: [String: [String: String]?]) {}
  275. }
  276. struct Foo {
  277. static func foo(↓↓input: [String: [String: String]?]?) {}
  278. }
  279. struct Foo {
  280. static func foo<K, V>(_ dict1: [K: V], _ dict2: [K: V]?) -> [K: V]
  281. }
  282. struct Foo {
  283. static func foo<K, V>(dict1: [K: V], dict2: [K: V]?) -> [K: V]
  284. }
  285. enum Foo {
  286. func foo(↓input: [String: String]?) {}
  287. }
  288. enum Foo {
  289. func foo(↓input: [String: [String: String]]?) {}
  290. }
  291. enum Foo {
  292. func foo(↓input: [String: [String: String]?]) {}
  293. }
  294. enum Foo {
  295. func foo(↓↓input: [String: [String: String]?]?) {}
  296. }
  297. enum Foo {
  298. func foo<K, V>(_ dict1: [K: V], _ dict2: [K: V]?) -> [K: V]
  299. }
  300. enum Foo {
  301. func foo<K, V>(dict1: [K: V], dict2: [K: V]?) -> [K: V]
  302. }
  303. enum Foo {
  304. static func foo(↓input: [String: String]?) {}
  305. }
  306. enum Foo {
  307. static func foo(↓input: [String: [String: String]]?) {}
  308. }
  309. enum Foo {
  310. static func foo(↓input: [String: [String: String]?]) {}
  311. }
  312. enum Foo {
  313. static func foo(↓↓input: [String: [String: String]?]?) {}
  314. }
  315. enum Foo {
  316. static func foo<K, V>(_ dict1: [K: V], _ dict2: [K: V]?) -> [K: V]
  317. }
  318. enum Foo {
  319. static func foo<K, V>(dict1: [K: V], dict2: [K: V]?) -> [K: V]
  320. }