Skip to content

Protocols with same name as class #213

Description

@AlexanderJ

If I use the same name for protocol and class in Objective-C (like many NS types, e.g. NSObject) the name of the protocol should get a Protocol suffix (Xcode also bridges objective-c protocols like that). This only applies if the code contains @protocol and @interface with the same exact name. Thus scanning all headers for this locally and sending the information to the translation is important.

Example: https://swiftify.me/lc8ui2

I would expect a Blub class and a BlubProtocol protocol.

Activity

  1. AlexanderJ commented on Mar 28, 2022

    @AlexanderJ
    Author

    I also reported this issue via email some time ago, but I didn't find a corresponding issue. I consider this high priority for us.

  2. alex-swiftify commented on Mar 28, 2022

    @alex-swiftify
    Member

    @AlexanderJ Copying over the reply that I've sent you in https://swiftify.zendesk.com/agent/tickets/2538
    The best I could suggest for now is renaming the protocols in your Objective-C code before the conversion.

    Protocol name matching class name.
    We may potentially rename such protocols appending a suffix i.e. Protocol, like with NSObject -> NSObjectProtocol.
    While this appears possible to implement, I foresee a potential problem with naming consistency among several source files within the project.

    For example, when you convert one pair of header/source files, it may contain a protocol name matching class name, thus the protocol will be renamed.
    However, if another source file (converted separately) references only the protocol name, but not the class name,
    the converter wouldn't know about the naming collision, thus the protocol won't be renamed.

    Hence I don't see an easy solution here, unfortunately.
    Processing absolutely all source files when you select and convert only 1-2 files within the project is only feasible in the Offline Converter,
    since sending the whole project to the server in the cloud version doesn't look like a feasible solution.

  3. AlexanderJ commented on Mar 29, 2022

    @AlexanderJ
    Author

    This is unfortunately not always possible. One of the FW used here JSONModel requires that protocol and class have the same name.

  4. alex-swiftify commented on Mar 29, 2022

    @alex-swiftify
    Member

    @AlexanderJ The case is understandable, but I don't see an easy or feasible solution here, unfortunately.
    Especially with each usage of "Blub", it's too difficult for the converter to infer whether a specific usage should be renamed to "BulbProtocol" or not.

    Scanning of all headers may be implemented at some point in time, but only in the Offline Converter version, since sending all headers to the API would be too time and bandwidth-consuming.

  5. AlexanderJ commented on Mar 30, 2022

    @AlexanderJ
    Author

    I don't think it would be necessary to transfer all the files. Scanning all files locally and transmitting only the information required to do the conversion correctly would suffice (e.g. just send the information that the class is available as protocol and as class). I don't think the code scanner is similar problematic to install on the customer site as the code converter.

    With this information you can translate NSArray and id as protocol and all other uses as class. That would at least compile. If I want to convert additional classes back to protocol I still have the option to do so manually.

  6. alex-swiftify commented on Mar 30, 2022

    @alex-swiftify
    Member

    @AlexanderJ Understandable, although implementing this looks pretty complex compared to the other improvements, so unfortunately I cannot guarantee this will be completed anytime soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions