内建响应函数
- response()
- responseData()
- responseString(encoding: NSStringEncoding)
- responseJSON(options: NSJSONReadingOptions)
- responsePropertyList(options: NSPropertyListReadOptions)
例子
responseString:
Alamofire.request(.GET, url).responseString { response in print("Success: \(response.result.isSuccess)") print("Response String: \(response.result.value)") }
输出:
// Success: true Response String: Optional("(b5drrjuy4yjj1j45jc5w1f45)")
response:
Alamofire.request(.GET, url).response { request, response, data, error in print(request) print(response) print(data) print(error) }
输出:
// Optional(<NSMutableURLRequest: 0x7fdd9a5d3db0> { URL: http://www.example.com }) Optional(<NSHTTPURLResponse: 0x7fdd9a5a2490> { URL: http://www.example.com } { status code: 200, headers { Connection = "keep-alive"; "Content-Encoding" = gzip; "Content-Type" = "text/html"; Date = "Wed, 02 Mar 2016 08:11:00 GMT"; Server = nginx; "Transfer-Encoding" = Identity; Via = "1.1.1.1"; } }) Optional(<28323161 76733466 716b7661 70726135 35697578 646c7262 7729>) nil
responseData:
Alamofire.request(.GET, url).responseData { response in print(response.request) print(response.response) print(response.result) }
输出:
// Optional(<NSMutableURLRequest: 0x7fc15b83f930> { URL: http://www.example.com }) Optional(<NSHTTPURLResponse: 0x7fc1597038c0> { URL: http://www.example.com } { status code: 200, headers { Connection = "keep-alive"; "Content-Encoding" = gzip; "Content-Type" = "text/html"; Date = "Wed, 02 Mar 2016 08:14:33 GMT"; Server = nginx; "Transfer-Encoding" = Identity; Via = "1.1.1.1"; } }) SUCCESS
responseJSON:
Alamofire.request(.GET, url).responseJSON { response in debugPrint(response) }
输出:
[Request]: <NSMutableURLRequest: 0x7ff8e0e2ceb0> { URL: http://1.jwxtapi.applinzi.com/getIDString.php } [Response]: <NSHTTPURLResponse: 0x7ff8e0e67150> { URL: http://1.jwxtapi.applinzi.com/getIDString.php } { status code: 200, headers { Connection = "keep-alive"; "Content-Encoding" = gzip; "Content-Type" = "text/html"; Date = "Wed, 02 Mar 2016 08:15:38 GMT"; Server = nginx; "Transfer-Encoding" = Identity; Via = "1.1.1.1"; } } [Data]: 26 bytes [Result]: FAILURE: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.} [Timeline]: Timeline: { "Request Start Time": 478599338.905915, "Initial Response Time": 478599339.333833, "Request Completed Time": 478599339.334189, "Serialization Completed Time": 478599339.334308, "Latency": 0.427917957305908 secs, "Request Duration": 0.428273975849152 secs, "Serialization Duration": 0.000119030475616455 secs, "Total Duration": 0.428393006324768 secs }
Alamofire.request(.GET, url).responseJSON { response in print(response.request) // original URL request print(response.response) // URL response print(response.data) // server data print(response.result) // result of response serialization if let JSON = response.result.value { print("JSON: \(JSON)") } }
输出:
Optional(<NSMutableURLRequest: 0x7fd69bf57ff0> { URL: http://www.example.com }) Optional(<NSHTTPURLResponse: 0x7fd69bc1dcc0> { URL: http://www.example.com } { status code: 200, headers { Connection = "keep-alive"; "Content-Encoding" = gzip; "Content-Type" = "text/html"; Date = "Wed, 02 Mar 2016 08:17:54 GMT"; Server = nginx; "Transfer-Encoding" = Identity; Via = "1.1.1.1"; } }) Optional(<28647377 6e617835 3531636b 70327171 33326134 35653434 3529>) FAILURE
时间: 2024-10-12 00:26:54