【问题标题】:What causes 'Constant captured by a closure before being initialized' error是什么导致“初始化之前由闭包捕获的常量”错误
【发布时间】:2016-07-10 11:54:06
【问题描述】:

在下面的课程中

class Foo {
   let _defaultValue = "N/A"
   let value: String 

   init (dict: NSDictionary) {
       self.value = dict["bar"] as? String! ?? _defaultValue
   }
}

编译器失败并显示消息 constant 'self.value' captured by a closure before being initialized

据我所知,没有运算符读取 `self.value。该消息似乎有些混乱。

我不小心想出了一个解决方法。我应该说这让我更加困惑:

class Foo {
       let value: String 

       init (dict: NSDictionary) {
           let _defaultValue = "N/A"
           self.value = dict["bar"] as? String! ?? _defaultValue
       }
    }

声明_defaultValue 并在构造函数中初始化它使代码编译。

如果有人能解释为什么在第一种情况下会发生错误以及在第二种情况下编译器更高兴的是什么,那将是一个巨大的帮助?

【问题讨论】:

    标签: swift swift2


    【解决方案1】:

    错误消息的原因是 nil-coalescing 运算符 被定义为

    public func ??<T>(optional: T?, defaultValue: @autoclosure () throws -> T) rethrows -> T
    

    并对第二个参数执行“自动关闭”(为了得到 短路行为)。所以

    self.value = dict["bar"] as? String ?? _defaultValue
    

    被编译器转换为

    self.value = dict["bar"] as? String ?? { self._defaultValue }()
    

    这里编译器抱怨因为self 之前被捕获 被完全初始化。 (错误信息略有不同 在 Swift 2 和 Swift 3 之间)。

    可能的解决方法。您可以先将属性分配给局部变量:

    init(dict: NSDictionary){
        let defValue = _defaultValue
        self.value = dict["bar"] as? String! ?? defValue
    }
    

    或者您可以将其设为类的静态属性:

    class Foo {
        static let _defaultValue = "N/A"
        let value: String
    
        init(dict: NSDictionary) {
            self.value = dict["bar"] as? String ?? Foo._defaultValue
        }
    }
    

    或者用 if 语句替换 ??

    class Foo {
        let _defaultValue = "N/A"
        let value: String
    
        init (dict: NSDictionary) {
            if let value = dict["bar"] as? String {
                self.value = value
            } else {
                self.value = _defaultValue
            }
        }
    }
    

    附录: 相关资源:

    引用错误报告:

    Jordan Rose:这是真的,因为 && 是使用 @autoclosure 实现的,但它肯定不是最理想的。

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 2019-10-04
      相关资源
      最近更新 更多