【问题标题】:swift programming NSErrorPointer error etc快速编程 NSErrorPointer 错误等
【发布时间】:2014-08-02 07:19:21
【问题描述】:
var data: NSDictionary = 
    NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: error) as NSDictionary;

这行代码给了我错误

NSError is not convertable to NSErrorPointer.

于是我想把代码改成:

var data: NSDictionary =
     NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: &error) as NSDictionary;

这会将 NSError 错误转换为 NSErrorPointer。但后来我得到一个新错误,无法理解它:

NSError! is not a subtype of '@|value ST4'

【问题讨论】:

    标签: swift xcode6 nserror


    【解决方案1】:

    自 Swift 1 以来,这些类型和方法发生了很大变化。

    1. NS 前缀已删除
    2. 这些方法现在抛出异常而不是获取错误指针
    3. 不鼓励使用 NSDictionary。而是使用 Swift 字典

    这会产生以下代码:

    do {
        let object = try JSONSerialization.jsonObject(
            with: responseData,
            options: .allowFragments)
        if let dictionary = object as? [String:Any] {
            // do something with the dictionary
        }
        else {
            print("Response data is not a dictionary")
        }
    }
    catch {
        print("Error parsing response data: \(error)")
    }
    

    的,如果你不关心具体的解析错误:

    let object = try JSONSerialization.jsonObject(
        with: responseData,
        options: .allowFragments)
    if let dictionary = object as? [String:Any] {
        // do something with the dictionary
    }
    else {
        print("Response data is not a dictionary")
    }
    

    原答案

    您的 NSError 必须定义为 Optional,因为它可以为 nil:

    var error: NSError?
    

    您还想说明解析中存在错误,该错误将返回 nil 或解析返回数组。为此,我们可以使用带有 as? 运算符的可选转换。

    这给我们留下了完整的代码:

    var possibleData = NSJSONSerialization.JSONObjectWithData(
        responseData,
        options:NSJSONReadingOptions.AllowFragments,
        error: &error
        ) as? NSDictionary;
    
    if let actualError = error {
        println("An Error Occurred: \(actualError)")
    }
    else if let data = possibleData {
       // do something with the returned data
    }
    

    【讨论】:

    【解决方案2】:

    如前所述,错误必须定义为可选 (https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/buildingcocoaapps/AdoptingCocoaDesignPatterns.html)

    但是 - 如果出现错误并返回 nil,此代码将崩溃,“As NSDictionary”将是罪魁祸首:

    var data: NSDictionary =
     NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: &error) as NSDictionary;
    

    你需要像这样进行 json 解析:

    var jsonError : NSError?
    
    let jsonResult : AnyObject? = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &jsonError)
    
    if let error = jsonError{
        println("error occurred: \(error.localizedDescription)")
    }
    else if let jsonDict = jsonResult as? NSDictionary{
        println("json is dictionary \(jsonDict)")
    }
    else if let jsonArray = jsonResult as? NSArray{
        println("json is an array: \(jsonArray)")
    }
    

    这会奏效。还要记住 json 可以作为数组返回。您可以传递任何您喜欢的选项,而不是传递 nil,例如:

    NSJSONReadingOptions.AllowFragments
    

    如果你喜欢。

    【讨论】:

      【解决方案3】:

      现在是 beta-3

      var error: AutoreleasingUnsafePointer<NSError?> = nil
      
      var data: NSDictionary = NSJSONSerialization.JSONObjectWithData( w, options:.AllowFragments, error: error ) as NSDictionary;
      

      【讨论】:

      • 谢谢白鲸,'?'是不必要的。所以,var 错误:NSErrorPointer = nil
      【解决方案4】:

      检查以下代码:

      var error: AutoreleasingUnsafePointer<NSErrorPointer?>=nil
      var dataVal: NSData =  NSURLConnection.sendSynchronousRequest(request1, returningResponse: response, error:nil)
      var err: NSError?
      var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
          println("Result\(jsonResult)")
      

      尝试使用

       var error: AutoreleasingUnsafePointer<NSErrorPointer?>=nil
      

      【讨论】:

        猜你喜欢
        • 2018-04-06
        • 1970-01-01
        • 2017-05-25
        • 2016-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多