I have an objective-c iOS codebase that uses a set of files generated (please don't laugh) by a tool from a SOAP WSDL file. One of the objects in the WSDL includes a property "Description". The tool dutifully - and correctly - translates this into an Objective-C object with a "Description" property. All has worked just fine for 15 years or so.
I'm in the process of converting my app to Swift because I can read the writing on the wall (despite Apple's protestations to the contrary). And the fact that swift can interact with Objective-C is a key reason that this is possible!
But alas...when accessing Objective-C code from Swift, the default is to convert the casing of the property from upper-case to camel case, so "Description" becomes "description", and conflicts with the Swift "description" property on such objective-c objects. (Does XCode raise a compile-time error on this? Of course not, but that's a separate digression). As a result, my code that references the "description" property gets the name of the class rather than the human-readable value in the "Description" (capital-D) property.
Apple recognizes this issue and provides the "NS_SWIFT_NAME" macro to specify a name to be used from Swift, and that could easily solve the problem, except that this is in code that's generated by a 3rd party tool, so it has no idea when/where to add the "NS_SWIFT_NAME" macro.
For now, I've got an insane hack on this: I've created an objective-c method visible to Swift that takes the object in question and returns the value of the "Description" field (capital-D), and that works, but (a) man that's an ugly hack, and (b) if my goal is to translate everything except the tool-generated-code to Swift, then...well, tough luck.
Any thoughts for solutions here? I can't seem to apply the "NS_SWIFT_NAME" macro in an extension or otherwise in a separate file...
I also thought about using an Objective-C category to extend the WDSL-generated class to provide another property for accessing the
Description. It's still messy, but I think it is cleaner than passing an object to a top-level function.Here's how I mocked it up.
First, here's the WSDL-generated class which you cannot change. It has a
Descriptionproperty:To provide access to the
Descriptionproperty under another name I created an Objective-C extension onWSDLClass:The bridging header needs to import
WDSLClass+Extensions.hto gain access to this. I can then do this in Swift:I.e. I can use the
getDescriptionproperty (from Swift or Objective-C) to access the Objective-CDescriptionproperty.Of course, you would need to rename
WSDLClassto whatever class name you need and maybegetDescriptionisn't the best name for the alias.If there are mutiple objects with a
Descriptionproperty then you would need to create an extension for each of them (best combined into a single file for simplicity).Granted you still need to keep a bit more Objective-C in your project, but it's a little easier to use than your current hacky workaround.
If
Descriptionisn't readonly then you would need to implement a custom getter and setter in the Objective-C category for getting/setting the underlyingDescriptionproperty.