When should we use neither [Weak Self] nor [Owned Self]?


Legonaftik

I read a few discussions on StackOverflow about when we should use [weak self] or [unowned self] go out of business.

However, we do n't need to use them in any case , because Swift doesn't show any errors or warnings when we use them explicitly selfinside a closure .

For example, should we use weakor unownedhere?

UIView.animate(withDuration: 0.3) {
    self.view.alpha = 0.0
}
shepherd david

You need to use [weak self] or [unnamed self] if closing could result in a "strong reference cycle".

If this can happen by assigning a capped property to self , you are referring to self or property self that closes itself. Closures are reference types, so essentially the same rules apply to strong references as you would with normal classes.

In the case of your example, no [weak self]or [unowned self], since you don't need to assign the closure to a variable inside the class you're selfreferencing , there won't be a strong reference cycle.

For more information, check out the Strong Closure Reference Loops section of the Swift Programming Language Guide . Here's an example from the above link when a strong reference cycle might be due to closures:

class HTMLElement {

    let name: String
    let text: String?

    lazy var asHTML: () -> String = {
        if let text = self.text {
            return "<\(self.name)>\(text)</\(self.name)>"
        } else {
            return "<\(self.name) />"
        }
    }

    init(name: String, text: String? = nil) {
        self.name = name
        self.text = text
    }

    deinit {
        print("\(name) is being deinitialized")
    }

}

If not [unowned self]in the closure asHTML, assigning to the closure will result in a strong reference cycle asHTML. Changing the implementation to the asHTMLfollowing fixed the issue:

lazy var asHTML: () -> String = {
    [unowned self] in
    if let text = self.text {
        return "<\(self.name)>\(text)</\(self.name)>"
    } else {
        return "<\(self.name) />"
    }
}

Related


When should we use neither [Weak Self] nor [Owned Self]?

Legonaftik I read a few discussions on StackOverflow about when we should use [weak self] or [unowned self] go out of business. However, we do n't need to use them in any case , because Swift doesn't show any errors or warnings when we use them explicitly self

When should we use neither [Weak Self] nor [Owned Self]?

Legonaftik I read a few discussions on StackOverflow about when we should use [weak self] or [unowned self] go out of business. However, we do n't need to use them in any case , because Swift doesn't show any errors or warnings when we use them explicitly self

When should we use neither [Weak Self] nor [Owned Self]?

Legonaftik I read a few discussions on StackOverflow about when we should use [weak self] or [unowned self] go out of business. However, we do n't need to use them in any case , because Swift doesn't show any errors or warnings when we use them explicitly self

When should we use neither [Weak Self] nor [Owned Self]?

Legonaftik I read a few discussions on StackOverflow about when we should use [weak self] or [unowned self] go out of business. However, we do n't need to use them in any case , because Swift doesn't show any errors or warnings when we use them explicitly self

Should I use [weak self] in PromiseKit blocks?

Sensual PromiseKit states the following on its website : Should I worry about retain cycles? tl;dr: It is safe to use self in promise handlers. This is safe: somePromise.then { self.doSomething() } If a somePromiseworkaround is provided, the function pass

Should I use [weak self] in PromiseKit blocks?

Sensual PromiseKit states the following on its website : Should I worry about retain cycles? tl;dr: It is safe to use self in promise handlers. This is safe: somePromise.then { self.doSomething() } If a somePromiseworkaround is provided, the function pass

Should I use [weak self] in PromiseKit blocks?

Sensual PromiseKit states the following on its website : Should I worry about retain cycles? tl;dr: It is safe to use self in promise handlers. This is safe: somePromise.then { self.doSomething() } If a somePromiseworkaround is provided, the function pass

Should I use [weak self] in PromiseKit blocks?

Sensual PromiseKit states the following on its website : Should I worry about retain cycles? tl;dr: It is safe to use self in promise handlers. This is safe: somePromise.then { self.doSomething() } somePromiseWhen parsing is provided , the function passed

Should I use [weak self] in PromiseKit blocks?

Sensual PromiseKit states the following on its website : Should I worry about retain cycles? tl;dr: It is safe to use self in promise handlers. This is safe: somePromise.then { self.doSomething() } If a somePromiseworkaround is provided, the function pass

Should I use [weak self] in PromiseKit blocks?

Sensual PromiseKit states the following on its website : Should I worry about retain cycles? tl;dr: It is safe to use self in promise handlers. This is safe: somePromise.then { self.doSomething() } If a somePromiseworkaround is provided, the function pass

When to use [self] vs [weak self] in a fast block?

Viswanath Deshmuk [self]is a new term that we can use in blocks to avoid using the self keyword. So [weak self]what's the difference ? Notice [self]the retention period? I couldn't find much information on this, so any simple example with explanation would be

When to use [self] vs [weak self] in a fast block?

Vishwanath Deshmukh [self]is a new term that we can use in blocks to avoid using the self keyword. So [weak self]what's the difference ? Notice [self]the retention period? I couldn't find much information on this, so any simple example with explanation would b

When to use [self] vs [weak self] in a fast block?

Vishwanath Deshmukh [self]is a new term that we can use in blocks to avoid using the self keyword. So [weak self]what's the difference ? Notice [self]the retention period? I couldn't find much information on this, so any simple example with explanation would b

When to use [self] vs [weak self] in a fast block?

Vishwanath Deshmukh [self]is a new term that we can use in blocks to avoid using the self keyword. So [weak self]what's the difference ? Notice [self]the retention period? I couldn't find much information on this, so any simple example with explanation would b

When to use [self] vs [weak self] in a fast block?

Vishwanath Deshmukh [self]is a new term that we can use in blocks to avoid using the self keyword. So [weak self]what's the difference ? Notice [self]the retention period? I couldn't find much information on this, so any simple example with explanation would b

When to use [self] vs [weak self] in a fast block?

Vishwanath Deshmukh [self]is a new term that we can use in blocks to avoid using the self keyword. So [weak self]what's the difference ? Notice [self]the retention period? I couldn't find much information on this, so any simple example with explanation would b

When to use [self] vs [weak self] in a fast block?

Vishwanath Deshmukh [self]is a new term that we can use in blocks to avoid using the self keyword. So [weak self]what's the difference ? Notice [self]the retention period? I couldn't find much information on this, so any simple example with explanation would b

When to use [self] vs [weak self] in a fast block?

Vishwanath Deshmukh [self]is a new term that we can use in blocks to avoid using the self keyword. So [weak self]what's the difference ? Notice [self]the retention period? I couldn't find much information on this, so any simple example with explanation would b