Ephemera Ephemera - 1 year ago 120
Swift Question

Using dispatch_async with self in Swift

I have run into this problem a few times while porting Objective-C code to Swift. Say I have the following code:

dispatch_async(dispatch_get_main_queue()) {

This will result in an error, underlining the entire
call, offering:

Could not find member 'addSubview'

I assume this is an error that has not yet been properly implemented because if I put the
call outside the
block, the project builds fine. Initially I assumed it may have something to do with capturing
in the block. However, inserting
[unowned self] in
results in the same error, as does
[weak self] in
(after the appropriate
unwrap operators have been inserted).

How can I get
blocks to work in Swift that need to capture

Answer Source

You should condition spinning off this action on the non-nullity, not test for it after you've already initiated it:

if let hostView = self.hostViewController?.view {
    dispatch_async(dispatch_get_main_queue()) {
} else {
    // handle nil hostView 

You should never unwrap an optional outside of an if let, or testing it first. Doing this should also resolve yer weak self issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download