isKnownUniquelyReferenced

func isKnownUniquelyReferenced<T : AnyObject>(_: inout T)

Returns a Boolean value indicating whether the given object is a class instance known to have a single strong reference.

The isKnownUniquelyReferenced(_:) function is useful for implementating the copy-on-write optimization for the deep storage of value types:

mutating func modifyMe(_ arg: X) {
    if isKnownUniquelyReferenced(&myStorage) {
        myStorage.modifyInPlace(arg)
    } else {
        myStorage = self.createModified(myStorage, arg)
    }
}

Weak references do not affect the result of this function.

This function is safe to use for mutating functions in multithreaded code because a false positive implies that there is already a user-level data race on the value being mutated.

object: An instance of a class. This function does not modify object; the use of inout is an implementation artifact. Returns: true if object is a known to have a single strong reference; otherwise, false.

Declaration

func isKnownUniquelyReferenced<T : AnyObject>(_ object: inout T) -> Bool
func isKnownUniquelyReferenced<T : AnyObject>(_: inout T?)

Returns a Boolean value indicating whether the given object is a class instance known to have a single strong reference.

The isKnownUniquelyReferenced(_:) function is useful for implementating the copy-on-write optimization for the deep storage of value types:

mutating func modifyMe(_ arg: X) {
    if isKnownUniquelyReferenced(&myStorage) {
        myStorage.modifyInPlace(arg)
    } else {
        myStorage = self.createModified(myStorage, arg)
    }
}

Weak references do not affect the result of this function.

This function is safe to use for mutating functions in multithreaded code because a false positive implies that there is already a user-level data race on the value being mutated.

object: An instance of a class. This function does not modify object; the use of inout is an implementation artifact. Returns: true if object is a known to have a single strong reference; otherwise, false. If object is nil, the return value is false.

Declaration

func isKnownUniquelyReferenced<T : AnyObject>(_ object: inout T?) -> Bool