Monday, June 6, 2016

Avoiding the Overuse of @objc in Swift

Swift 2.2’s #selector syntax introduces a new compiler error:

Argument of '#selector' refers to a method that is not exposed to Objective-C.

Fix-it   Add '@objc' to expose this method to Objective-C

Jesse Squires:

We cannot simply add @objc to this method in the original ViewControllerType protocol for a number of reasons. If we do, then the entire protocol needs to be marked @objc, which means[…] Our example here is simple, but imagine a much more complex object graph that makes heavy use of Swift’s value types and a hierarchy of three protocols with this one in the middle. Introducing @objc as the fix-it suggests would break the entire world in our app.

[…]

We can decompose this protocol by separating out all of the @objc code into its own protocol. Then, we can use protocol composition to reunite them.

Comments RSS · Twitter

Leave a Comment