I’ve been doing iOS and Mac OS programming in Swift for a while and I noticed some “pitfalls”. They are some syntax details that may cause compiler errors and leave you wonder, “why is that”. I believe that all the “pitfalls” here are included in the Swift official documentation and tutorials already. But I think it would help myself and other people if I put them all together here in one place.
1. Naming of methods
The following code won’t work:
Fix:
2. Exposing method to Objective-C
In Swift 3.0, the following code won’t compile:
Error message would be Argument of '#selector' refers to instance method 'buttonTapped' that is not exposed to Objective-C'
Fix by adding @objc to the method:
Thanks to reader Jon Showing for their input.
3. Single line expression in closures: Implicit ‘return’
Fix by either adding @discardableResult to the method,
or
4. Closures that take no parameters and return Void