3
    apiFunc(user: User.currentUser, start: 0, limit: Constants.numberOfItemInOnePage,
        success: { [weak self] (friends) -> Void in
            dispatch_async(dispatch_get_main_queue(), { [weak self] () -> Void in
                if let strongSelf = self {
                    strongSelf.friendList = friends
                    strongSelf.loading = false
                    strongSelf.tableView.reloadData()
                }
            })
        }, failure: nil)

error

Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1

happens when compile the code above, but if I remove the 2nd [weak self], error disappears

    apiFunc(user: User.currentUser, start: 0, limit: Constants.numberOfItemInOnePage,
        success: { [weak self] (friends) -> Void in
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                if let strongSelf = self {
                    strongSelf.friendList = friends
                    strongSelf.loading = false
                    strongSelf.tableView.reloadData()
                }
            })
        }, failure: nil)

I think as there are 2 closures, it should be 2 [weak self], anyone knows why compile error happens

newme
  • 577
  • 6
  • 17

1 Answers1

1

You don't have to repeat [weak self] in nested closures in the same way as @weakify or __weak self patterns in Objective-C.

[weak self] in Swift automatically creates the same pattern by the compiler, and the weak self is defined by the outer closure is used by the inner closure.

Here is the corresponding question for Objective-C version: iOS blocks and strong/weak references to self

Community
  • 1
  • 1
Yoichi Tagaya
  • 4,547
  • 2
  • 27
  • 38