LoginSignup
5
0

More than 5 years have passed since last update.

[WEAK SELF] & [UNOWNED SELF] & LAZY VAR

Last updated at Posted at 2018-05-18

INTRODUCTION

OKKK! Today We are going to discuss about [WEAK SELF] and [UNOWNED SELF]. You may wonder what are those right? Ok , let's me explain. If we talk about [weak self] and [unowned] , we will think of Closure and Retain Cycle Problem . Well , what is closure?

I. Closure

Closure are self-contained blocks of functionality that can be passed around and used in your code.It can capture and store references to any constants and variables from the context in which they are defined. for more detail you can visit swift official documentation.
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

II. Retain Cycle Problem

You may think "ouuuhh Retain Cycle Problem ?To make it easy to understand , you just think about the memory leak in your application. What is memory leak? In swift , there is an automatic memory management which it deallocate the object from the memory automatically but there is time which the automatic memory management could not deallocate the object and that cause the retain cycle problem. There are many way that cause that memory leak problem and by using closure is one of them.

III."OK SO WHAT IS [WEAK SELF] & [UNOWNED SELF]?"

We use [weak self] and [unowned self] to solve the memory leak which cause by the closure. For example

class Example {

    private var counter = 0
    private var closure : (() -> ()) = { }

    init() {
        closure = {
            self.counter += 1     <= this self will make a memory leak
            print(self.counter)
        }
    }

    func foo() {
        closure()
    }

}

To solve the above problem we can use [unowned self] & [weak self]

[UNOWNED SELF]

class Example {

    private var counter = 1

    private var closure : (() -> ()) = { }

    init() {
        closure = { [unowned self] in
            self.counter += 1
            print(self.counter)
        }
    }

    func foo() {
        closure()
    }
}

Now the closure doesn't have a strong reference anymore. Just be careful when using [unowned self] since that, if the object has already been deallocated when the closure is called, a crash will occur.

[WEAK SELF]

class Example {

    private var counter = 1

    private var closure : (() -> ()) = { }

    init() {
        closure = { [weak self] in
            self?.counter += 1 //
            print(self?.counter ?? "")
        }
    }

    func foo() {
        closure()
    }

}

[weak self] achieves the same result as [unowned self] but is handled as an optional.

Yes that's it. for more detail and reference.
https://medium.com/mackmobile/avoiding-retain-cycles-in-swift-7b08d50fe3ef

5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0