how to make a weak pointer to self in swift outside of a block

user1709076

i want to make a weak pointer to self in swift like how we used to in objective-c like

 __weak Something *weakself = self;

I have found people explaining how to use a 'weak self' inside a block,

    { in [unowned self] ...}

but i dont want to define 'weakself' inside my block, i want to define weakself outside of blocks

Martin R

Just define a weak reference with the weak keyword:

weak var weakSelf = self

From the documentation:

You indicate a weak reference by placing the weak keyword before a property or variable declaration.
...
NOTE: Weak references must be declared as variables, to indicate that their value can change at runtime. A weak reference cannot be declared as a constant.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Make self weak in methods in Swift

Swift: Maintaining atomicity in a block-based execution using weak self

Swift - weak self in didSet

Why would I not use a weak pointer to self in a Block passed to dispatch_after()?

weak self in gcd in swift closure

How to make new pointer in Swift?

Always pass weak reference of self into block in ARC?

Referring to weak self inside a nested block

Swift closures [weak self] and async tasks

Swift guarding weak self for nested callbacks

Is it the right way using `[weak self]` in swift closure?

How to make animation end when the mouse outside of the common block?

Swift delegation - when to use weak pointer on delegate

How to access data outside the Alamofire block using swift 3.0

How to make thread safe code block in swift

Objective-C: Should we use weak self within block or assign weak self to strong before using it?

When to use [self] vs [weak self] in swift blocks?

How to make weak references in Java?

How can I make a weak protocol reference in 'pure' Swift (without @objc)

Why make_shared call the destructor even if there are still weak pointer?

Should I use [weak self] if I don't actually reference self in my completion block?

Swift: Assign a class method to a handler and handling weak self

Swift weak reference going nil even when self keeps existing

For swift references within view model, should self reference be weak or unknown?

Should I use [weak self] when calling an API in Swift

How to access a variable outside the block?

Will weak pointer in a block become dangling when I refer it with stong pointer in OC?

How to make weak set or weak map iterable in ES6?

Difference between weak self vs weak self()