xcode 找不到接受 Swift 中提供的参数的“init”的重载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26442059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not find an overload for “init” that accepts the supplied arguments in Swift
提问by hightech
I am trying to figure out how to translate this in Swift and I am also having this error: "Could not find an overload for “init” that accepts the supplied arguments". Any suggestion appreciated. Thanks.
我试图弄清楚如何在 Swift 中翻译它,但我也遇到了这个错误:“找不到接受所提供参数的‘init’的重载”。任何建议表示赞赏。谢谢。
var pageImages:[UIImage] = [UIImage]()
pageImages = [UIImage(named: "example.png"), UIImage(named: "example2.png")]
回答by Eugene Braginets
Confirming what matt says:
确认马特所说的:
in xCode 6.0 this does work:
在 xCode 6.0 中,这确实有效:
images = [UIImage(named: "steps_normal"), UIImage(named: "steps_big")]
but in xCode6.1 values should be unwrapped:
但在 xCode6.1 中,值应该被解包:
images = [UIImage(named: "steps_normal")!, UIImage(named: "steps_big")!]
回答by matt
Unwrap those optionals. A UIImage is not the same as a UIImage?, which is what the named:
initializer returns. Thus:
打开这些选项。UIImage 与 UIImage? 不同,这是named:
初始化程序返回的内容。因此:
var pageImages = [UIImage(named: "example.png")!, UIImage(named: "example2.png")!]
(Unless, of course, you actually wantan array of optional UIImages.)
(当然,除非您确实想要一组可选的 UIImages。)
回答by Nate Cook
UIImage(named:)
changed to be a failable initializerin Xcode 6.1, which means that it will return nil
if any of the images you've listed are missing from your bundle. To safely load the images, try something like this instead:
UIImage(named:)
在 Xcode 6.1 中更改为可失败的初始化程序,这意味着nil
如果您列出的任何图像从您的包中丢失,它将返回。要安全加载图像,请尝试以下操作:
var pageImages = [UIImage]()
for name in ["example.png", "example2.png"] {
if let image = UIImage(named: name) {
pageImages.append(image)
}
}