作者:mobiledu2502877207 | 来源:互联网 | 2023-05-26 13:55
下面是我用来构建图像数组的代码
var _displayImage : Array!
required init(coder aDecoder: NSCoder) {
self._displayImage = []
}
func buildImageSet() {
self._displayImage = [UIImage(named: "image1"), UIImage(named: "image2"), UIImage(named: "image3"), UIImage(named: "image4"), UIImage(named: "image5")]
}
我收到一个错误:找不到接受提供的参数的'init'的重载
我究竟做错了什么?
1> Logan..:
问题是你声明你的数组是这样的:
var _displayImage : Array!
所以它期待UIImage
对象.问题是UIImage(named:String)
返回一个UIImage?
可选项.快速修复是切换这个:
func buildImageSet() {
self._displayImage = [UIImage(named: "image1"), UIImage(named: "image2"), UIImage(named: "image3"), UIImage(named: "image4"), UIImage(named: "image5")]
}
对此:
func buildImageSet() {
self._displayImage = [UIImage(named: "image1")!, UIImage(named: "image2")!, UIImage(named: "image3")!, UIImage(named: "image4")!, UIImage(named: "image5")!]
}
虽然这没有考虑到任何这些图像的可能性nil
.如果任何图像无法正确加载,您的应用程序将崩溃.如果你确定它们都存在并且将永远存在,这应该没问题.:)