我正在使用SwiftLint,我需要在"{"后面有一行新行,例如,如果有一个body,这是正确的

    func foo() {
        if true { }
    }

但这是错误的

    func foo() {
        if true { print("This print should be on the new line") }
    }

like this

    func foo() {
        if true { 
          print("This print should be on the new line") 
        }
    }

怎么做?

推荐答案

似乎SwiftLint对于任何单个字符(.)都有点脆弱,所以我得到的最接近的是这个

custom_rules:
  newline_block:
    name: "Newline Block"
    regex: "\\{[ ]*(\\S|( +))+[ ]*\\}"
    message: "Statement must be on its own line"

更好的正则表达式应该是{[ ]*(.+)+[ ]*}(未替换),但由于某种原因,当我在Xcode上运行它时,它不起作用.

这可以满足您的打印请求,但该解决方案确实存在一些缺点:

func foo() {
    // No warning
    if true {}
    // Newline Block Violation: Statement must be on its own line (newline_block)
    if true { }
}

但我不确定这是否也适用于你:

var connection: OpaquePointer? {
    // Newline Block Violation: Statement must be on its own line 
    get { queue.sync { underlyingConnection } }
    // Newline Block Violation: Statement must be on its own line 
    set { queue.sync { underlyingConnection = newValue } }
}

Swift相关问答推荐

编写Swift字符串的属性包装时遇到问题

在MacOS上检测键盘是否有Globe键

在 RealityKit 中查找特定模型的 findEntity(named:) 方法的替代方法?

Swift // Sprite Kit:类别位掩码限制

swift 是否遇到 Java 的 2gb 最大序列化大小问题?

Swift 非参与者隔离闭包

将 struct 作为泛型类型传递并访问该泛型类型属性

如何确定扩展中的具体类型?

有什么方法可以快速为泛型参数分配默认值?

如何让动画变慢 SwiftUI

(SwiftLint)如果有正文,如何编写规则(可能是自定义),在{之后总是\n(换行)?

为什么更改属性后动画会加速? SwiftUI

在 ViewController 的 UICollectionView 中拉取刷新

Swift 协议只能设置?

Swift中switch 盒的详尽条件

如何在 GCD 中停止 DispatchWorkItem?

Swift:创建 UIImage 数组

不能从非开放类继承 swift

Xcode6 中的 Swift 类与 Cocoa Touch 类

如何从 iOS 应用程序中删除场景委托?