【问题标题】:SwiftyJSON & Swift 3: Cannot convert return expression of type 'Int32?' to return type > 'Int?'SwiftyJSON 和 Swift 3:无法转换“Int32”类型的返回表达式?返回类型 > 'Int?'
【发布时间】:2017-02-10 00:35:27
【问题描述】:

我们正在升级到带有 CocoaPods 配置 pod 'SwiftyJSON', '3.1.0' 的 SwiftyJSON Swift 3。

我们收到此错误:

/Users/xxx/Documents/iOS/xxx/Pods/SwiftyJSON/Source/SwiftyJSON.swift:866:33: 无法转换类型为“Int32?”的返回表达式?返回类型 '诠释?'

错误出现在 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 的问题?

【问题讨论】:

    标签: cocoapods swift3 swifty-json


    【解决方案1】:

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

    pod 'SwiftyJSON', '3.0.0'

    【讨论】:

    • 您是否运行了 pod update 命令并让它完成?并进行全面清理和构建?
    【解决方案2】:

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

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

    【讨论】:

    • 有效但我们的目的不是修改客户端库源。
    • 是的。我同意你的看法。我们不应该修改它。但是如果我们想要临时解决方案来消除错误,我们可以使用上面的代码。将来,我们将明确删除它。
    【解决方案3】:

    试试这个,

    错误:返回 self.number?.int32Value

    好:返回 self.number?.intValue

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2016-10-25
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-18
        • 1970-01-01
        相关资源
        最近更新 更多