【问题标题】:Xcode 7.0 update issue with Swift 2Swift 2 的 Xcode 7.0 更新问题
【发布时间】:2015-09-25 22:20:28
【问题描述】:

当我将我的 Xcode 更新到最新版本时,我刚刚遇到了一个用 Swift 编写的项目的问题; Xcode 7.0。

Xcode 告诉我应该转换我的代码以使用 Swift 的新语法 (Swift 2)。

问题是我无法理解如何解决某些代码行。

举个例子:我在我的应用程序中使用了 SwiftyJSON,Xcode 给了我这个错误,如下所示:

public struct JSON {

    public init(data:NSData, options opt: NSJSONReadingOptions = .AllowFragments, error: NSErrorPointer = nil) {
        if let object: AnyObject = NSJSONSerialization.JSONObjectWithData(data, options: opt, error: error) {
            self.init(object)
        } else {
            self.init(NSNull())
        }
    }

【问题讨论】:

  • 您看到的错误是什么?

标签: ios xcode swift swift2


【解决方案1】:

一些函数丢弃了一些参数,在这种情况下是错误指针。相反,它会抛出(这意味着您只能通过 try catch 处理来调用它)。这是您的代码的 swift 2.0 版本

public struct JSON 
{

    public init(data:NSData, options opt: NSJSONReadingOptions = .AllowFragments, error: NSErrorPointer = nil) 
    {
        do {
            let object:AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: opt)
            self.init(data: object as! NSData)
        } catch {
            //error handling code
        }
    }
}

【讨论】:

  • 不胜感激;)
猜你喜欢
  • 1970-01-01
  • 2016-08-22
  • 2015-12-05
  • 1970-01-01
  • 2015-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多