其他答案可以使用,另一个选项是使用反引号.这样做不需要定义strongSelf,并允许使用自己,而不必打开可选的自我.
let c: () -> Void = { [weak self] in guard let `self` = self else { throw NSError(domain: "self was destroyed",code: 1,userInfo: nil) } self.doSomethingNonOptionalSelf() }