iosswiftcopy-on-write

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?


Solution

  • 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.