简体   繁体   English

无法识别的选择器已通过Swift发送到实例

[英]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. 我试图与幻灯片应用程序swift ,但我有一个问题,在main.storyboard我添加一个imageview和一个按钮,当此按钮,用户点击幻灯片将动画。

I wrote this code at viewController.swift 我在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. 在appdelegate.swift,我没有编写任何代码。

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'

UIImageView 's animationImages property should be an array of UIImage objects. UIImageViewanimationImages属性应该是UIImage对象的数组。 What you're actually setting it to is an array of an array of UIImage objects. 您实际上将其设置 UIImage对象数组的数组 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. 当ImageView开始尝试对图像进行动画处理时,它期望其数组中的每个元素都是一个UIImage,但它会查找另一个数组。 It tries to invoke a UIImage selector, _isResizable , on an array object, and that's the error you're seeing: 它尝试在数组对象上调用UIImage选择器_isResizable ,这是您看到的错误:

-[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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM