I am trying to use specific capabilities not available in the available iOS SDK for Parse Platform (Server), but that I know are available with the REST API. Specifically to use a DISTINCT query.
Using the Parse Dashboard and REST API client app on my dev computer (Rested.app), I have verified the following query completes as expected:
curl -X GET \
-H "X-Parse-Application-Id: someAppID" \
-H "X-Parse-Master-Key: someKey" \
-G \
--data-urlencode "distinct=TeeTime" \
http://somehost:1337/parse/aggregate/CompEntry
Which successfully returns data:
{ "results": [ { "__type": "Date", "iso": "2020-08-29T07:00:00.000Z" }, { "__type": "Date", "iso": "2020-08-29T07:09:00.000Z" } ] }
The original data is from, which has 3 rows, 2 of which share the same TeeTime:
And a screenshot of the output from the Rested.app:
Now I am trying to convert this for my Swift / iOS project.
I am trying to move the downloaded data into a new struct to represent the object(s), using the Codable/Decodable approach and matching the JSON property names. The code I have so far is below (placed some comments inline too). The Struct definitions occur in separate .swift files, but so long as outside the main ViewController definition.
struct TeeTimeData: Codable {
let results: [Results]
}
struct Results: Codable {
let __type: String
let iso: String // TODO: THIS SHOULD BE A DIFFERENT DATA TYPE - I HAVE PICKED HARDER DATA TYPE TO START WITH!
}
Then within the main ViewController struct:
class ViewController: UIViewController {
@IBAction func buttonGetTeeTimes(_ sender: UIButton) {
if let url = URL(string: "http://somehost:1337/parse/aggregate/CompEntry") {
var request = URLRequest(url: url)
request.addValue("someAppID", forHTTPHeaderField: "X-Parse-Application-Id")
request.addValue("someKey", forHTTPHeaderField: "X-Parse-Master-Key")
request.httpMethod = "GET"
let params = ["distinct": "TeeTime"] // TODO: THIS VAR IS NOT ACTUALLY BEING TIED TO THE REQUEST EITHER - 2nd PROBLEM...
let session = URLSession(configuration: .default)
let requestTask = session.dataTask(with: request) { (data, response, error) in
if error != nil {
print("API Error: \(error)")
}
if let dataUnwrap = data {
// TODO: MOVE THIS INTO NEW CLASS (DataModel, etc)
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(TeeTimeData.self, from: dataUnwrap)
print(decodedData)
} catch {
print("Decode Error: \(error)")
}
}
}
requestTask.resume()
}
}
}
And the console output is:
Decode Error: keyNotFound(CodingKeys(stringValue: "__type", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "results", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: "__type", intValue: nil) ("__type").", underlyingError: nil))
My first guess is the 2 underscores, "__type", at the start of the property definition?


__typein your response JSON. - gcharitaprint("Decode Error: \(error)")=>print("Decode Error: \(error)"); print("With response: \(String(data: data, encoding: .utf8))")might help. - Larme"--data-urlencode "distinct=TeeTime"Doesn't it means that it'd add&distinct=TeeTimeto the URL? You aren't doing it in your code, no? So maybe that's why the response is different (and why you might want to show the returned data as String). - Larme?distinct=TeeTime, sorry, typo. Check en.wikipedia.org/wiki/Query_string and developer.apple.com/documentation/foundation/urlqueryitem (and related StackOverflow questions if needed) to build it with your parameters. - Larme