【问题标题】:Cannot find 'isUniquelyReferencedNonObjC' in scope在范围内找不到“isUniquelyReferencedNonObjC”
【发布时间】:2021-04-18 15:59:38
【问题描述】:
import Foundation

final class Ref<T> {
  var val : T
  init(_ v : T) {val = v}
}

struct Box<T> {
    var ref : Ref<T>
    init(_ x : T) { ref = Ref(x) }

    var value: T {
        get { return ref.val }
        set {
          if (!isUniquelyReferencedNonObjC(&ref)) {
            ref = Ref(newValue)
            return
          }
          ref.val = newValue
        }
    }
}

为什么上面的代码编译时没有出现“Cannot find 'isUniquelyReferencedNonObjC' in scope”错误?

【问题讨论】:

    标签: ios swift copy-on-write


    【解决方案1】:

    看起来 isUniquelyReferencedNonObjC 在 swift 5 中已被弃用

    您必须使用 isKnownUniquelyReferenced(_:) 进行写时复制优化。

    Apple docs 为您的案例提供了代码示例。

    【讨论】:

      猜你喜欢
      • 2020-11-23
      • 2021-06-18
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多