4

I'm having issues with initialization of a custom class. I need to set up an observer on some data when the class is initialized which updates properties. Initially, the properties can be empty and that is how I set them at initiation. However, Xcode still throws the "'self' captured by a closure before all members were initialized" error. Here is a shortened version of the code.

class Foo {
    init() {
        self.usersRef = ref.child("users")
        self.usersRef.observe(DataEventType.value, with: { (snapshot) in
            // snapshot error checking
            // users is [String]

            self.users = users
        })
    }

    private var usersRef: DatabaseReference
    private(set) var users: [String] = []

}

I have also tried

class Foo {
    init() {
        self.users = [String]()
        self.usersRef = ref.child("users")
        self.usersRef.observe(DataEventType.value, with: { (snapshot) in
            // snapshot error checking
            // users is [String]

            self.users = users
        })
    }

    private var usersRef: DatabaseReference
    private(set) var users: [String]

}

to ensure initialization prior to the callback.

From this question it seemed I only needed to give the properties initial values, but this does not seem to be the case. I would prefer not to have to call another function to set up these handlers after initialization.

Thank you for an help

2
  • where do you initialize usersRef variable?
    – Vyacheslav
    Commented Jun 15, 2017 at 7:06
  • @Vyacheslav Thank you, I missed that in the question, fixed it. Commented Jun 15, 2017 at 7:07

1 Answer 1

3

Try to use this block

 { [unowned self] (snapshot) in
            // snapshot error checking
            // users is [String]

            self.users = users
        })

or

{ [weak self] (snapshot) in
            // snapshot error checking
            // users is [String]

            self?.users = users
        })
4
  • I had seen the [unowned self] while searching for a solution, but i'm not fully understanding its purpose. From what I understand it is creating a weak reference? Will this create other possible issues I should look out for in error checking? Commented Jun 15, 2017 at 7:09
  • @JustinOroz I have not faced huge problems while using those wrappers
    – Vyacheslav
    Commented Jun 15, 2017 at 7:14
  • Thank you, it compiles fine, and I can't see any issues arising from a weaker reference, so this works great! I'll mark it as accepted, thank you. Commented Jun 15, 2017 at 7:18
  • Basically a strong self will add to the retain count, thus it will always point to itself while it exists, creating a retain-cycle. That's why you want(ed) a weak reference
    – Stephen J
    Commented Jun 8, 2018 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.