【问题标题】:Swift 3: Expression type [UIImage] is ambiguous without more contextSwift 3:表达式类型 [UIImage] 在没有更多上下文的情况下是模棱两可的
【发布时间】:2017-01-23 17:41:40
【问题描述】:

我尝试创建新的 UIImage 对象数组:

var images = [UIImage](repeating: nil, count: 10)
for i in 0..<10
    {
        images[i] = ...
    }

但我收到了这个错误(在第一行): 表达式类型 [UIImage] 在没有更多上下文的情况下不明确

【问题讨论】:

    标签: ios arrays swift swift3 uiimage


    【解决方案1】:

    您试图用UIImage 对象声明一个数组,然后用nil 实例化。试试

    var images = [UIImage?](repeating: nil, count: 10)
    

    然后在您访问它们时处理它们为零的图像。

    如果您已经知道将如何填充图像,则可以将 map 函数用于数组,如下所示:

    var images = (0..<10).map { (i) -> UIImage in
        return UIImage() // however you are trying to get the UIImage
    }
    

    【讨论】:

    • @LeoDabus 也可以!我刚刚提出了optional 建议以提高效率。您不想分配将立即被丢弃的对象。
    • 我认为正确的方法是创建一个空数组var images = [UIImage](),然后将新图像附加到他的循环中,或者按照您的建议使用 map 或 forEach (0..&lt;10).forEach { images.append(UIImage(named:"image\($0)") ?? UIImage()) }
    猜你喜欢
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多