Swift: ObjC runtime and overloading

This one comes up surprisingly often over at #swift-lang.

Screen Shot 2015-06-10 at 5.49.49 PM

From the 1.2 release notes:

Swift now detects discrepancies between overloading and overriding in the Swift type system and the effective behavior seen via the Objective-C runtime. (18391046, 18383574)

If you’re working along with the Stanford video, this is why your code is failing.

Update: If you see “conflicts with previous declaration with the same Objective-C selector”, you’ve overloaded an ObjC method. Solution: Change the name of one of them.

But wait, you say, I haven’t overloaded anything. There’s just one version of that method:

  • Are you using an extension?  Is there a method already defined for that selector? Rename yours.
  • Did you add a property to an NSObject subclass and then add a setProperty method using that property’s name (e.g. var player, setPlayer())? Rename the property or the function.

Comments are closed.