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

SwiftyJSON & Swift 3: Cannot convert return expression of type 'Int32?' to return type > 'Int?'

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

我们收到此错误:

/Users/xxx/Documents/iOS/xxx/Pods/SwiftyJSON/Source/SwiftyJSON.swift:866:33: Cannot convert return expression of type 'Int32?' to return type 'Int?'

SwiftyJSON.swift 中的 return 语句出错:

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 的问题吗?

意识到 SwiftyJSON 的 supported version 是 3.0.0 而不是 3.1.0。使用 3.0.0,问题消失了。

pod 'SwiftyJSON', '3.0.0'

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

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

试试这个,

Bad : return self.number?.int32Value

Good : return self.number?.intValue

Reason : Seems to be more generic in how it can return integers.

我手动添加了 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()
        }
    }
}