ios - type()不符合协议(protocol)

标签 ios swift

我不知道原因,但我无法检查 UIImage 是否为 nil,即代码

        var arrayBuildingImage: [UIImage] = []

        for(var i = 0; i <= 100; i++){

            var path:String = self.getDocumentsDirectory().stringByAppendingPathComponent("building/\(i).png")

            if ( arrayBuildingImage[i] = UIImage(contentsOfFile: path)? ) {//ERROR HERE

            }
            else{
                break
            }

        }

错误:

Type() 不符合协议(protocol)'BooleanType'

我正在使用 Swift 1.1

最佳答案

你应该检查它是否为零:

let img : UIImage? = UIImage(contentsOfFile: path)
if img != nil {}

或者使用“if let”语句:

if let img = UIImage(contentsOfFile: path) {}

以一种更“敏捷”的方式:

guard let img = UIImage(contentsOfFile: path) else { return }
// Do you stuff here

关于ios - type()不符合协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34038291/

相关文章:

Swift 无法使用类型为 '*' 的参数列表调用 '(Int, Int)'

json - swift 。如何从Firebase数据库的快照中获取特定数据

ios - 如何使用 Storyboard 以编程方式更改 rootViewController?

ios - 如何让程序填写文本字段,并确保用户无法编辑程序填写的内容?

iphone - 按型号定位 iPhone 应用程序(例如 3G 与 3GS)

iphone - iOS 通用应用程序中的自定义头文件

ios - 是否可以无线调试 iphone webapp?

swift - 是否存在确定类型应该用字符串初始化的协议(protocol)?

ios - 如何像这个截图一样在 Swift 中设置半圆的 UIImage

ios - 调试合并.swiftmodule : segmentation fault: 11