dstepan dstepan - 1 month ago 13
Swift Question

Debugging on iPhone Parses JSON Values Differently vs. Simulator

I'm having an issue where my table view is getting populated with what looks to be chinese characters when I run the app on my iPhone. However, when I run the app through simulator, I get the expected (English) result. Below are screenshots of the issue.

Running in Simulator
Running in Simulator

Running on iPhone
Running on iPhone

Below is the code that populates the objects

private func populateArtists(_ maxRecordsToPopulate: Int, json: JSON) {

let artists = json["artists"]["items"]

for i in 0 ..< artists.count {

if i < maxRecordsToPopulate {

let artist = Artist()

print(artists[i]["name"].string!)

artist.name = artists[i]["name"].string!
artist.detailUrl = artists[i]["href"].string!
artist.id = artists[i]["id"].string!

self.searchResults.artists.append(artist)

}

}

}


When I place a breakpoint inside, it looks like the JSON values being returned are being parsed differently.

Output from app running in Simulator
Output from app running in Simulator

Output from app running on iPhone
Output from app running on iPhone

I thought this might be a localization setting or something somewhere on the phone, but have no clue how to find out if that's the case. I've tried deleting the app from the phone and reinstalling and that didn't help either.

Frameworks I'm using are Facebook SDK, Alamofire, and Swifty-JSON, in case any of those could be causing this weird issue.

Answer

Usually this is an issue with the Accept-Language header in the HTTP request. Is the device set to another language? If so, it will default to that language. You can manually change the header value by using an NSMutableURLRequest and changing that value as described here.