Fixing dispatch_async error in Swift 3/4

If you are copying some older Swift code (say circa 2014/15 or older) off the web to ensure some code is executed on the main queue, such as like this:

dispatch_async(dispatch_get_main_queue(), {
//do something that must be done on the main queue
})

You will be greeted with these 2 errors by Xcode:

‘dispatch_async’ has been replaced by instance method ‘DispatchQueue.async(execute:)’
‘dispatch_get_main_queue()’ has been replaced by property ‘DispatchQueue.main’

And unfortunately Xcode doesn’t offer to “fix” this for you automatically as it often does with other method calls that have been deprecated.

The new way to perform such a call is simply this:

DispatchQueue.main.async {
//do something that must be done on the main queue
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s