简体   繁体   中英

Alamofire 4 Request is returning NSArray, cannot figure out how to parse with SwiftyJSON in Swift 3

The previous version of this Alamofire .POST request and SwiftyJSON parsing was working fine with Swift 2.2 and Xcode 7. Updated to Swift 3.0, which required install of updated Alamofire (4.0.0) and updated SwiftyJSON. After some syntax adjustments, everything now compiles.

The problem is that my web app now appears to return an NSArray, whereas before, when the code worked, a nearly identical Request got an NSData return that SwiftyJSON would parse. The following code shows the Request:

   Alamofire.request("https://www.mywebappurl", method: .post, parameters: parameters)
   .responseJSON { (response:DataResponse<Any>) in

                    if let data = response.result.value as? Data {

The data variable is never assigned because the response type is not NSData. Tried to cast to that type by changing the last line to this:

    let data = response.result.value as! Data

That version compiles fine, but as soon as you trigger the Request you get an error: Could not cast value of type '__NSArrayI' (0x105a37c08) to 'NSData'

Note that the request is returning data as expected. And in the previous Alamofire this data was NSData without any action being taken to convert it. Since it appears from the aforementioned error that the returned data is an array already, has it already been parsed by Alamofire? Or is there something that can be done to make SwiftyJSON parse it like it was parsed before?

EDIT

Since the current type being returned is an NSArray, and the web app is sending an array, it seems possible that the SwiftyJSON parsing is no longer necessary? Tried the following code:

Alamofire.request("https://www.mywebappurl", method: .post, parameters: parameters)
.responseJSON { (response:DataResponse<Any>) in

let testdata = response.result.value as! NSArray
print(testdata[0])

Which yielded this output in the Xcode "All Output" screen:

{
  1 = "08/01/16";
  2 = 285;
  3 = 160;
}

It's not clear to me if that means testdata is an array of arrays, an array of dictionaries, or an array of unparsed strings. So as an alternative to answering the question of "How can one obtain an NSData response from this Alamofire request", a response to the question of "What one line of code, if any, can be used to obtain the integer value 285 from the NSArray testdata shown above?" would also resolve the problem.

If you change the data initialization line from this:

if let data = response.result.value as? Data {

to this:

let data = response.data

Then the request results in an NSData return that can be parsed by SwiftyJSON as it was before. The working request and parsing code looks like this:

Alamofire.request("https://www.mywebappurl", method: .post, parameters: parameters)
.responseJSON { (response:DataResponse<Any>) in

let data = response.data
let jsonvalues = JSON(data: data!)

Some error handling might be appropriate to add, but that's not related to the question at hand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM