iosswiftobjective-c

Convert this obj-c method signature to swift


I have the following method signature in Obj-C:

- (void)handleOpenUrl:(NSURL * _Nullable)url options:(NSDictionary * _Nullable)options

And to my mind, the Swift version should be:

func handleOpenUrl(_ url: URL?, options: [AnyHashable: Any]?)

Im trying to mock a library by using an extension to make conform to a protocol containing the above signature. But my Swift version doesnt work. Can anyone help me as to why? Most common error is:

Unavailable instance method 'handleOpenUrl(_:options:)' was used to satisfy a requirement of protocol 'xxxLibProtocol'


Solution

  • The actual Swift signature generated for this ObjC function is:

    func handleOpen(_ url: URL?, options: [AnyHashable : Any]?)
    

    If a method name has a suffix that matches the name of the first parameter, this suffix is removed from the name of the Swift method, as it would otherwise violate Swift's API Design Guidelines.

    In the Xcode editor of an ObjC file, you can also display the "Generated Interface" for Swift. Just click the "Related Items" Button and Select "Generated Interface". Apple describes this in the Technote TN3108.

    If you can change the code of the libraries, you may also change the Swift method signatures with NS_REFINED_FOR_SWIFT.