简体   繁体   中英

Cannot find 'isUniquelyReferencedNonObjC' in scope

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
        }
    }
}

Why doesn't the code above compile with "Cannot find 'isUniquelyReferencedNonObjC' in scope" error?

Looks like isUniquelyReferencedNonObjC is deprecated in swift 5

You must use isKnownUniquelyReferenced(_:) for copy on write optimization.

Apple docs has an examples of code for your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM