Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Swift, will future new APIs be overridden by user-defined methods in existing apps on the App Store?

In Objective-C, for example, if Apple adds new method called method1 to UIView, existing apps which are already released to the App Store and use the following code could 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 a override keyword to prevent overriding accidentally. If you override a method without override keyword, the compiler generates compile-time error.

What will happen if Apple adds new API methods in the next iOS versions, and if my apps or your apps use methods whose name are the same as the new APIs' names.

In Swift, will new API methods be overridden by methods in existing apps like Objective-C?
Or new APIs don't effect existing same-name user-defined methods thanks to Swift's explicit override feature (override keyword)?

like image 691
user_ Avatar asked Nov 09 '22 23:11

user_


1 Answers

If your app is already built and uploaded no you won't have an issue.

But if you try to re-build the app for a new update and you have the bad luck they named a new Api Method the same name of your Object Method, most likely the error you might get is only if the place you use the method you don't use a correct identifier like Not calling self.method1() and just calling method1() and your object inherits from UIViewController, which coincidently has that new Method1.

other than that I wouldn't worry about that type of issue happening, well at least I haven't had an issue like this the 3-4 years I have been programming 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 a documentation about it or did you tested it yourself in Swift?

I have tested it myself because I have Apps in the App Store. So what happens is that whatever is already uploaded the code will be working because the App you uploaded prepackages the Frameworks with their current working Apis and your Classes. And No, I have not seen a Documentation about it, I know about it because I have personally see it.

2) And I think you can't define func method1() without a override keyword if UIViewController already has func method1()

Thats correct! Assuming the APi has a method already you have the write Override keyword to be able to use that function with the same name. But remember according to your scenario you mentioned that the API created that method with that name AFTER you had already your project uploaded in the AppStore. So the problem you would only see it when doing some new coding and trying to Rebuild the app.

like image 123
S.H. Avatar answered Nov 14 '22 23:11

S.H.