Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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
Ios 如何从Alamofire-swift解析upload.responseJSON?_Ios_Swift_Alamofire - Fatal编程技术网

Ios 如何从Alamofire-swift解析upload.responseJSON?

Ios 如何从Alamofire-swift解析upload.responseJSON?,ios,swift,alamofire,Ios,Swift,Alamofire,我正在使用alamofire将文件上载到服务器。我成功上传了这些文件。我想帮助解析上传完成时触发的JSON响应。在上传文件的主UIViewController中,我设置了一个通知。在第二个UIViewcontroller(我使用它作为自定义弹出窗口来显示进度条)中,我设置了一个观察者来解析responseJSON。我不确定如何解析这个JSON响应。我已经采取了一些试探性的步骤 UIViewController 1 // 'responseJSON' FIRES WHEN IPLOAD IS

我正在使用alamofire将文件上载到服务器。我成功上传了这些文件。我想帮助解析上传完成时触发的JSON响应。在上传文件的主UIViewController中,我设置了一个通知。在第二个UIViewcontroller(我使用它作为自定义弹出窗口来显示进度条)中,我设置了一个观察者来解析responseJSON。我不确定如何解析这个JSON响应。我已经采取了一些试探性的步骤

UIViewController 1

 // 'responseJSON' FIRES WHEN IPLOAD IS
                upload.responseJSON { response in
                    debugPrint(response)

                    NotificationCenter.default.post(name: uploadFinishName, object: nil, userInfo: ["response": response])
                }
UIViewcontroller 2

  NotificationCenter.default.addObserver(forName: uploadFinishName, object: nil, queue: OperationQueue.main) { (notification) in

        print("notification.userInfo: \(notification.userInfo)")

        guard
        let userinfo = notification.userInfo as? [String:[AnyHashable:Any]],
        let json = userinfo["response"] as? [AnyHashable:Any] else{
                print("problem")
                return
        }

        print("json: \(json)")


    }
例如,在上面的代码中,我从控制台返回“problem”

答复如下:

 [Request]: POST myDomain/dev/imgpdfuploadrecieve.php
 [Response]: <NSHTTPURLResponse: 0x1c0235860> { URL: myDomain/dev/imgpdfuploadrecieve.php } { Status Code: 200, Headers {
"Content-Length" =     (
    12
);
"Content-Type" =     (
    "text/html; charset=UTF-8"
);
Date =     (
    "Tue, 04 Sep 2018 10:39:32 GMT"
);
Server =     (
    "Apache/2.4.33 (Amazon) PHP/7.2.5"
);
"x-powered-by" =     (
    "PHP/7.2.5"
);
} }
[Data]: 12 bytes
[Result]: SUCCESS: {
code = 104;
}
[Timeline]: Timeline: { "Request Start Time": 557750367.595, "Initial 
Response Time": 557750367.601, "Request Completed Time": 557750372.791, 
"Serialization Completed Time": 557750372.793, "Latency": 0.007 secs, "Request Duration": 5.196 secs, "Serialization Duration": 0.002 secs, "Total Duration": 5.199 secs }
[请求]:POST myDomain/dev/imgpdfuploadrecieve.php
[响应]:{URL:myDomain/dev/imgpdfuploadrecieve.php}{状态代码:200,标题{
“内容长度”=(
12
);
“内容类型”=(
“text/html;字符集=UTF-8”
);
日期=(
“2018年9月4日星期二10:39:32 GMT”
);
服务器=(
“Apache/2.4.33(Amazon)PHP/7.2.5”
);
“x-powered-by”=(
“PHP/7.2.5”
);
} }
[数据]:12字节
[结果]:成功:{
代码=104;
}
[时间线]:时间线:{“请求开始时间”:557750367.595,“初始
响应时间:557750367.601,“请求完成时间”:557750372.791,
“序列化完成时间”:557750372.793,“延迟”:0.007秒,“请求持续时间”:5.196秒,“序列化持续时间”:0.002秒,“总持续时间”:5.199秒}

响应不是JSON,而是Alamofire
DataResponse
对象。请使用快速帮助(⌥-单击)或转到定义(⌃⌘-单击)以查看其properties@vadian-你说得对。然而,当我键入“response.result”时,我只得到“SUCCESS”。如何访问“code=104”?它是一个Alamofire
Result
type,其中包含一个带有关联类型的枚举。再次请阅读@vadian,如果您想提交上述答案,我可以接受。将来可能会帮助其他有类似问题的人。非常感谢!