如何在Swift 2.0中“强化”使用后卫的可选自我

时间:2022-09-11 18:30:16

There's a similar question about how to weakify/strongify self, which is answered, but I'm wondering how to use "self" without rightward-drifting caused by if let:

关于如何削弱/强化自我有一个类似的问题,这是有回答的问题,但我想知道如何使用“自我”而没有向右漂移,如果让:

Welcome to Apple Swift version 2.0 (700.0.59 700.0.72). Type :help for assistance.
  2> import Foundation
  3> class Foo {
  4.     func guardOptSelf() -> () throws -> Void {
  5.         return { [weak self] in
  6.             guard let self = self else { throw NSError(domain: "I was destroyed!", code: 1, userInfo: nil) }
  7.             self.doSomethingNonOptionalSelf()         
  8.         }
  9.     }
  10. }
repl.swift:6:19: error: pattern matching in a condition requires the 'case' keyword
            guard let self = self else { throw NSError(domain: "I was destroyed!", code: 1, userInfo: nil) }
                  ^
                  case
repl.swift:6:23: error: binary operator '~=' cannot be applied to two 'Foo?' operands
            guard let self = self else { throw NSError(domain: "I was destroyed!", code: 1, userInfo: nil) }

2 个解决方案

#1


31  

You can shadow self; you just need backticks to indicate that "you know what you're doing". For example:

你可以暗影自我;你只需要反叛来表明“你知道你在做什么”。例如:

foo.doSomethingAsyncWithBar(bar) { [weak self] result in
    guard let `self` = self else { return }
    self.receivedResult(result)
}

Or, in your example:

或者,在您的示例中:

2> import Foundation
3> class Foo {
4.     func guardOptSelf() -> () throws -> Void {
5.         return { [weak self] in
6.             guard let `self` = self else { throw NSError(domain: "I was destroyed!", code: 1, userInfo: nil) }
7.             self.doSomethingNonOptionalSelf()         
8.         }
9.     }
10. }

#2


7  

Because guard let `self` = self is a compiler bug as stated by Chris Lattner I would try to avoid it. In your example you can use just simple optional chaining:

因为护卫让'self` = self是一个编译器错误,如Chris Lattner所说,我会尽量避免它。在您的示例中,您可以使用简单的可选链接:

return { [weak self] in
    self?.doSomethingNonOptionalSelf()         
}

Sometimes you might need to use self as a parameter. In such case you can use flatMap (on an optional type):

有时您可能需要使用self作为参数。在这种情况下,您可以使用flatMap(在可选类型上):

{ [weak self] in
    self.flatMap { $0.delegate?.tableView($0, didSelectRowAt: indexPath) }
}

In case you need to do something more complicated you can use if let construct:

如果你需要做一些更复杂的事情,你可以使用if let:

{ [weak self] in
    if let strongSelf = self {
        // Do something more complicated using strongSelf
    }
}

or guard let construct:

或保护让我们构建:

{ [weak self] in
    guard let strongSelf = self else { return }

    // Do something more complicated using strongSelf
}

or you can create a private method:

或者您可以创建一个私有方法:

{ [weak self] in
    self?.doSomethingMoreComplicated()
}

...

private func doSomethingMoreComplicated() {
    // Do something more complicated
}

#1


31  

You can shadow self; you just need backticks to indicate that "you know what you're doing". For example:

你可以暗影自我;你只需要反叛来表明“你知道你在做什么”。例如:

foo.doSomethingAsyncWithBar(bar) { [weak self] result in
    guard let `self` = self else { return }
    self.receivedResult(result)
}

Or, in your example:

或者,在您的示例中:

2> import Foundation
3> class Foo {
4.     func guardOptSelf() -> () throws -> Void {
5.         return { [weak self] in
6.             guard let `self` = self else { throw NSError(domain: "I was destroyed!", code: 1, userInfo: nil) }
7.             self.doSomethingNonOptionalSelf()         
8.         }
9.     }
10. }

#2


7  

Because guard let `self` = self is a compiler bug as stated by Chris Lattner I would try to avoid it. In your example you can use just simple optional chaining:

因为护卫让'self` = self是一个编译器错误,如Chris Lattner所说,我会尽量避免它。在您的示例中,您可以使用简单的可选链接:

return { [weak self] in
    self?.doSomethingNonOptionalSelf()         
}

Sometimes you might need to use self as a parameter. In such case you can use flatMap (on an optional type):

有时您可能需要使用self作为参数。在这种情况下,您可以使用flatMap(在可选类型上):

{ [weak self] in
    self.flatMap { $0.delegate?.tableView($0, didSelectRowAt: indexPath) }
}

In case you need to do something more complicated you can use if let construct:

如果你需要做一些更复杂的事情,你可以使用if let:

{ [weak self] in
    if let strongSelf = self {
        // Do something more complicated using strongSelf
    }
}

or guard let construct:

或保护让我们构建:

{ [weak self] in
    guard let strongSelf = self else { return }

    // Do something more complicated using strongSelf
}

or you can create a private method:

或者您可以创建一个私有方法:

{ [weak self] in
    self?.doSomethingMoreComplicated()
}

...

private func doSomethingMoreComplicated() {
    // Do something more complicated
}