WeakReference

public final class WeakReference<T> : NSObject

Allows weakly reference another object.

final class MyClass {
   // MARK: - Private Properties
   private let weakReference: WeakReference<MyOtherClass>

   // MARK: Public Functions
   func foo() {
       guard let reference = self.weakReference.value else {
           return
       }
       // do something with reference which is non-nil
   }

   // MARK: Initializers
   init(myOtherClass: MyOtherClass) {
       self.weakReference = WeakReference(myOtherClass)
   }
}

Public Properties

  • Returns the weakly referenced value.

    Declaration

    Swift

    public var value: T? { get }

Initializers

  • Creates an instance that weakly references the provided value.

    Declaration

    Swift

    public init(_ value: T)

    Parameters

    value

    The object to weakly reference

    Return Value

    The instance