SwiftyJSON & Swift 3:无法将 'Int32?' 类型的返回表达式转换为返回类型 > 'Int?'



我们正在使用CocoaPods config pod 'SwiftyJSON', '3.1.0'升级到SwiftyJSON Swift 3。

我们得到这个错误:

/用户/xxx/文件/iOS/xxx/豆荚/SwiftyJSON/源/SwiftyJSON.swift: 866:33:不能转换Int32类型的返回表达式?返回类型"Int ?'

SwiftyJSON.swift:

返回语句出错
public var int: Int? {
    get {
        return self.number?.int32Value
    }
    set {
        if let newValue = newValue {
            self.object = NSNumber(value: newValue)
        } else {
            self.object = NSNull()
        }
    }
}

有人知道是什么问题吗?这是我们的CocoaPods配置或SwiftyJSON的问题吗?

我只是用下面的代码替换了一行代码。简单的

public var int: Int? {
        get {
            return self.number?.intValue
        }
        set {
            if let newValue = newValue {
                self.object = NSNumber(value: newValue)
            } else {
                self.object = NSNull()
            }
        }
    }

意识到SwiftyJSON支持的版本是3.0.0而不是3.1.0。使用3.0.0,问题消失了。

pod 'SwiftyJSON', '3.0.0'

试试这个,

返回self.number .int32Value

Good: return self.number?

原因:在返回整数方面似乎更通用。

我手动添加as? Int以使其再次工作:

public var int: Int? {
    get {
        return self.number?.int32Value as? Int
    }
    set {
        if let newValue = newValue {
            self.object = NSNumber(value: newValue)
        } else {
            self.object = NSNull()
        }
    }
}

最新更新