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

More Xcode 9 Oddities – Cannot load underlying module for [some pod]

File this under more Xcode 9 oddities – a viewcontroller can work perfectly fine one moment, then the next out of the blue, Xcode will throw the following sort of error:

xcode-oddities-1

A “Product > Clean” seems to take care of this easily enough, but really hope Apple can get Xcode bugs like these sorted out by the next revision.

 

Xcode 9 Oddities – External Keyboard Not Working in Simulator

Since “upgrading” to Xcode 9 (specifically v9.3 as of time of writing), I’ve noticed quite a number of annoying oddities – or call them bugs. Today’s nuisance is how the external keyboard (i.e. the physical keyboard you type into) will occasionally just stop working. As in, typing into your physical keyboard will not send any input into the IOS app you are running in the simulator.

As with many things tech related, the good ol’ restart seems to be “fix” it – as in just Quit the simulator and re-run your project.

Not too big a deal, but really, stuff like this just shouldn’t be happening. Come on Apple, we deserve better!