【问题标题】:Swift: self in initializer has type (T) -> () -> TSwift:初始化器中的 self 具有类型 (T) -> () -> T
【发布时间】:2020-09-14 23:06:59
【问题描述】:

TL;DR:

NSObject 的self 变量如何获取(T) -> () -> T 类型?

备注

我可以看到为什么这样使用self 是不合法的。但我正在尝试理解第二条错误消息。

守则

struct DummyStorer {
    let dummy: Dummy
}

struct Dummy {
    let storer = DummyStorer(dummy: self)
    // Use of unresolved identifier 'self'
    // OK, that's reasonable. But...
}

class Dummy : NSObject {
    let storer = DummyStorer(dummy: self)
    // Cannot convert value of type '(Dummy) -> () -> Dummy' to expected argument type 'Dummy'
    // ... how does the compiler arrive at this?
}

【问题讨论】:

    标签: swift types initialization


    【解决方案1】:

    它是NSObjectProtocol的一部分

    public protocol NSObjectProtocol {
    
    
        func isEqual(_ object: Any?) -> Bool
    
        var hash: Int { get }
    
    
        var superclass: AnyClass? { get }
    
    
        func `self`() -> Self         // << here !!
    

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      相关资源
      最近更新 更多