K. Zeng
K. Zeng

Reputation: 13

Error occurred after code migration from swift 2.0 to swift 3.0

Can anyone help me resolving this error, just look at the following code.

override func viewDidLoad() {
  super.viewDidLoad()
  ...
  ...

  //the following line occurs error: "Ambiguous use of 'startAnimation'"
  **perform(#selector(UIViewAnimating.startAnimation), with: nil, afterDelay: 0.3)**
}

How can I fix the above issue? enter image description here

Upvotes: 1

Views: 94

Answers (2)

Rakesh
Rakesh

Reputation: 29

Use UIView class method.

+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations



   UIView.animate(withDuration:duration){    
            // code to be executed    
     }

look at this UIView method documentation https://developer.apple.com/documentation/uikit/uiview/1622515-animatewithduration and similar query Whats the Swift 3 animateWithDuration syntax?

Upvotes: 0

deadbeef
deadbeef

Reputation: 5563

UIViewAnimating already has a method to start the animation after a delay, you should use this one instead :

startAnimation(afterDelay: 0.3)

If you insist on using perform(_:with:afterDelay:) you can use it like so:

perform(NSSelectorFromString("startAnimation"), with: nil, with: 0.3)

But keep in mind that you loose compiler safety with this approach, and if you make a typo in your selector your app will crash.

Also, your code is weird because UIViewController doesn't conform to UIViewAnimating, so unless you implemented the protocol yourself (which would be quite unusual), it's not going to work. It's more likely that you have a method called startAnimation in your view controller and that the Swift migrator mistakenly added the reference to UIViewAnimating. In which case the correct code would be:

perform(#selector(self.startAnimation), with: nil, afterDelay: 0.3)

Upvotes: 1

Related Questions