Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
swift编程错误、内部错误等_Swift_Xcode6_Nserror - Fatal编程技术网

swift编程错误、内部错误等

swift编程错误、内部错误等,swift,xcode6,nserror,Swift,Xcode6,Nserror,这行代码给了我一个错误 var data: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: error) as NSDictionary; 于是我想把代码改成: NSError is not convertable to NSErrorPointer. 这会将NSError错误转换为NSErr

这行代码给了我一个错误

var data: NSDictionary = 
    NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: error) as NSDictionary;
于是我想把代码改成:

NSError is not convertable to NSErrorPointer.
这会将NSError错误转换为NSErrorPointer。但我又犯了一个新错误,无法理解:

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

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

  • NS
    前缀被删除
  • 这些方法现在抛出异常,而不是使用错误指针
  • 不鼓励使用词典。相反,使用Swift字典
  • 这将产生以下代码:

    NSError! is not a subtype of '@|value ST4'
    
    当然,如果您不关心特定的解析错误:

    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)")
    }
    

    原始答案

    您的N错误必须定义为
    可选
    ,因为它可以为零:

    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")
    }
    
    您还需要说明解析中存在错误,该错误将返回
    nil
    ,或者解析返回数组。为此,我们可以使用带有
    as?
    操作符的可选强制转换

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

    var error: NSError?
    
    检查以下代码:

    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
    }
    
    var错误:autoreleasingusafepointer=nil
    var dataVal:NSData=NSURLConnection.sendSynchronousRequest(请求1,返回响应:响应,错误:无)
    变量错误:n错误?
    var jsonResult:NSDictionary=NSJSONSerialization.JSONObjectWithData(dataVal,选项:NSJSONReadingOptions.MutableContainers,错误:nil)作为NSDictionary
    println(“结果\(jsonResult)”)
    
    试用

    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错误:autoreleasingusafepointer=nil
    
    如前所述,错误必须定义为可选()

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

     var error: AutoreleasingUnsafePointer<NSErrorPointer?>=nil
    
    您需要像这样进行json解析:

    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)")
    }
    
    如果您愿意。

    现在进入beta-3

    NSJSONReadingOptions.AllowFragments
    
    var错误:autoreleasingusafepointer=nil
    变量数据:NSDictionary=NSJSONSerialization.JSONObjectWithData(w,选项:.AllowFragments,错误:error)作为NSDictionary;
    
    如果解析json时出错,并且返回nil,则此操作将崩溃。请参阅此答案:我更新了答案以防止解析错误导致崩溃Hanks moby,“?”是不必要的。因此,var错误:NSErrorPointer=nil
    var error: AutoreleasingUnsafePointer<NSError?> = nil
    
    var data: NSDictionary = NSJSONSerialization.JSONObjectWithData( w, options:.AllowFragments, error: error ) as NSDictionary;