mays
mays

Reputation: 13

Unrecognized selector sent to instance with Swift

I'm trying to make a slideshow app with swift but I have a problem, in main.storyboard I add one imageview and one button, when the user click on the button the slideshow will animate.

I wrote this code at viewController.swift

@IBOutlet var ImageView: UIImageView!

@IBOutlet var animateBtn: UIButton!

@IBAction func animateBtnClicked(sender: UIButton) {
    startAnimation()
}

var imageList:[UIImage]=[]

override func viewDidLoad() {

    super.viewDidLoad()
   for i in 1 ... 3{
        let imagename="\(i).jpg"

    imageList.append(UIImage(named: imagename)!)
    }

}

func startAnimation()->(){
    if !ImageView.isAnimating()
    {
    ImageView.animationImages=[imageList]
        ImageView.startAnimating()
    animateBtn.setTitle("Stop Animation", forState:UIControlState.Normal)}
    else
    {
        ImageView.stopAnimating()
    }

}

At appdelegate.swift I didn't write any code.

But this app was crashed when I clicked the button and show this message error

2014-12-02 09:54:55.693 #4 animation photo[921:613] -[Swift._NSSwiftArrayImpl _isResizable]: unrecognized selector sent to instance 0x7f82126615d0
2014-12-02 09:54:55.698 #4 animation photo[921:613] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift._NSSwiftArrayImpl _isResizable]: unrecognized selector sent to instance 0x7f82126615d0'

Upvotes: 1

Views: 4213

Answers (1)

Matt Gibson
Matt Gibson

Reputation: 38238

UIImageView's animationImages property should be an array of UIImage objects. What you're actually setting it to is an array of an array of UIImage objects. That's because you've already got an array:

var imageList:[UIImage]=[]

...but when you set the property, you wrap this in square brackets, which puts the existing array into another, further array:

ImageView.animationImages=[imageList]

When the ImageView starts trying to animate the images, it expects each element in its array to be a UIImage, but it instead finds another array. It tries to invoke a UIImage selector, _isResizable, on an array object, and that's the error you're seeing:

-[Swift._NSSwiftArrayImpl _isResizable]: unrecognized selector sent to instance 0x7f82126615d0

So, just don't wrap your array with an array. Set it directly as the property:

ImageView.animationImages = imageList

Upvotes: 2

Related Questions