Objective-C method signatures match after Swift conversion

I have two methods in my objective-c library that, after converting to Swift, seem to have the same method signature. They look like this: Objective-C:

- (void)doSomething:(UIViewController *)viewController __attribute__((deprecated)); - (BOOL)doSomething:(UIViewController *)viewController error:(NSError **)error; 

From what I read about Swift conversion of methods using NSErrors + BOOL return values ​​in Objective-C, these methods will have the following Swift method signatures:

 func doSomething(viewController: UIViewController) func doSomething(viewController: UIViewController) throws 

Since the first call is not really recommended in Objective-C, is there a way to force the calls made for this method to use the second signature (so that I can take advantage of the errors received)?

I am trying to call a method as follows:

 do { try myObjectInstance.doSomething(self) } catch let error as NSError { print(error) } 

This approach, apparently, by default refers to the first declaration, which gives me a warning that: a) the method is outdated and b) there is nothing to catch, because there are no errors.

+5
source share
1 answer

You can use the NS_SWIFT_UNAVAILABLE macro to make it inaccessible to Swift:

 - (void)doSomething:(UIViewController *)viewController __attribute__((deprecated)) NS_SWIFT_UNAVAILABLE("use the throwing variant"); 

Then the Swift compiler will select the only available method, which will be different.

(In my tests, this seems to violate autocomplete - you can specify an error about it.)

0
source

Source: https://habr.com/ru/post/1232124/


All Articles