46

I just started programming and following a tutorial online I was unable to create this animation. Can anyone tell me why it's saying:

Nil is not compatible with expected argument type UIViewAnimationOptions

and how to fix it?

view.addSubview(myFirstLabel)

UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.0, initialSpringVelocity: 0.0, options: nil, animations: {

    self.myFirstLabel.center = CGPoint(x: 100, y:40 + 200)

}, completion: nil)
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Brenner
  • 655
  • 1
  • 6
  • 20
  • [UIView transitionWithView:self duration:0.3 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{ //code to change the image of UIButton } completion:^(BOOL finished) { self.userInteractionEnabled = true; }]; options cannot be nil – GameLoading Nov 07 '15 at 08:36

3 Answers3

124

You may replace options: nil with options: [] should make the error goes way.

starball
  • 20,030
  • 7
  • 43
  • 238
AaoIi
  • 8,288
  • 6
  • 45
  • 87
  • 4
    Starting from Swift 2 the `UIViewAnimationOptions` is a struct that implements the `OptionSetType` protocol. That is why you should pass an empty set like `options: []` instead of nil. – sgl0v Oct 22 '15 at 15:35
  • wasted a lot of time looking for the answer and it worked. Thanks – Singh Dec 31 '15 at 10:04
0

UIViewAnimationOptions is an enum backed by integers. You should pass 0. Here is the doc for the enum.

Jelly
  • 4,522
  • 6
  • 26
  • 42
  • 4
    Unfortunately this doesn't work in swift 2. "Cannot convert value of type 'Int' to expected argument type 'UIViewAnimationOptions'". `options: []` works fine – JakubKnejzlik Oct 07 '15 at 13:42
0

It's because UIViewAnimationOptions is an OptionSet type, not Optional type OptionSet according to apple

You use the OptionSet protocol to represent bitset types, where individual bits represent members of a set.

it's mainly used to create a combined flag from the current flags inside the set, in your case animation flags or types we can call them, this will give you the ability to combine options to make the final desired option, there are about 23 option, however in your case you can just pass an empty OptionSet as []

Mohmmad S
  • 5,001
  • 4
  • 18
  • 50