Will future new APIs be redefined in Swift by user methods in existing applications in the App Store?

In Objective-C, for example, if Apple adds a new method called method1 to UIView, existing applications that are already released in the App Store and use the following code may crash or behave unexpectedly:

 // Objective-C @interface MyView : UIView - (void)method1; @end // Swift class MyView : UIView { func method1() { // do something } } 


But in Swift, to override a method, you need the override keyword to prevent accidental overrides. If you override a method without the override keyword, the compiler generates a compile-time error.

What happens if Apple adds new API methods in future versions of iOS, and if my applications or your applications use methods that have the same name as the new APIs.

In Swift, new API methods will be overridden by methods in existing applications, such as Objective-C?
Or do the new APIs not affect existing user methods of the same name due to the Swift override keyword function?

+5
source share
1 answer

If your application has already been created and downloaded, you will not have a problem.

But if you try to rebuild the application for a new update, and you are out of luck, he called the new Api method the same name as your object method, most likely you can get an error only if the place where you use the method in which you are not using the correct one an identifier, for example Not call self.method1 () and just call method1 (), and your object is inherited from the UIViewController, which has the new Method1 matching it.

Other than that, I would not have to worry about this type of problem, but at least I didn’t have the same problem as the 3-4 years that I programmed for iOS.

 //Lets assume UIViewController has a new Method in the Api now which //they updated called Method1 class MyViewController :UIViewController { init { //When you try to re-build your app this line of code right here would complain //because of ambiguity, two methods called the same one from your //Parent Class and your own Class. //method1() //"super.method1()" or code below to solve the ambiguity issue. self.method1() } //Added this just because I happen to use UIViewController. override viewDidLoad() { super.viewDidLoad() } //Your own method without Override, since you want to use your own method. func method1() { //Does something important } } 

Update Based on your comment:

1) Is there any documentation about this or have you tested it yourself in Swift?

I myself tested this because I have Applications on the App Store. So what happens is that no matter what is already loaded, the code will work, because the application you downloaded prepackages Frameworks with their current working Apis and your classes. And no, I have not seen the Documentation about it, I know about it because I personally see it.

2) And I think that you cannot define func method1 () without the override keyword if the UIViewController already has func method1 ()

Right! Assuming APi has a method, you have the Override keyword to write to be able to use this function with the same name. But remember, according to your scenario, you mentioned that the API created this method with this name AFTER you have already loaded your project into the AppStore. Thus, the problem you would see only when performing a new encoding and trying to rebuild the application.

+1
source

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


All Articles