nsjsonserialization

Swift, NSJSONSerialization and NSError

无人久伴 提交于 2020-01-22 13:21:26
问题 The problem is when there is incomplete data NSJSONSerialization.JSONObjectWithData is crashing the application giving unexpectedly found nil while unwrapping an Optional value error instead of informing us using NSError variable. So we are unable to prevent crash. You can find code we are using below var error:NSError? = nil let dataToUse = NSJSONSerialization.JSONObjectWithData(receivedData, options: NSJSONReadingOptions.AllowFragments, error:&error) as NSDictionary if error != nil {

Swift - define a recursive type with a protocol

青春壹個敷衍的年華 提交于 2020-01-14 03:48:09
问题 I want to define a type that can be serialized to a valid JSON object So for instance if a JSON can contain the following: String Number Array Object Date Boolean I would like to define a protocol with valid types protocol JsonSerializable { } typealias JSONObject = [String : JsonSerializable] typealias JSONArray = [JsonSerializable] // foundation implements serializing numbers + strings + dates etc. to JSON extension String : JsonSerializable {} extension Int : JsonSerializable {} // problem

JSON parsing using NSJSONSerialization in iOS

Deadly 提交于 2020-01-09 07:10:54
问题 I am parsing a JSON in my code. But I am getting some unexpected issues while retrieving data of parsed JSON . So let me explain my problem. I have to parse following JSON data using xcode. This is what data to be parsed looks like while I hit same URL in browser: { "RESPONSE":[ {"id":"20", "username":"john", "email":"abc@gmail.com", "phone":"1234567890", "location":"31.000,71.000"}], "STATUS":"OK", "MESSAGE":"Here will be message" } My code to reach up to this JSON data is as follow: NSData

iOS6 Sort JSON objects

为君一笑 提交于 2020-01-07 05:44:04
问题 In my json file I have a title , subtitle , and url . I sort the title to set the items alphabetically, but the url isn't sorted with the title and I don't know why. This is what i've done: NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; NSArray *arrayOfItems = [allDataDictionary objectForKey:@"items"]; for (NSDictionary *diction in arrayOfItems) { NSString *titles = [diction objectForKey:@"title"]; NSString

Google API Key issues with Google Places

穿精又带淫゛_ 提交于 2020-01-06 19:57:06
问题 I'm working on an app where I need to query places from the google places API and save the nearest place to the user's google account (they sign into the app through GIDSignIN). The Places API is activated in the console and they key is correct, however, the URL might be problematic and I keep getting this error... Optional(["error_message": This IP, site or mobile application is not authorized to use this API key. Request received from IP address 2607:f598:b349:fd01:94f8:7386:d82c:1b29, with

Google API Key issues with Google Places

霸气de小男生 提交于 2020-01-06 19:56:04
问题 I'm working on an app where I need to query places from the google places API and save the nearest place to the user's google account (they sign into the app through GIDSignIN). The Places API is activated in the console and they key is correct, however, the URL might be problematic and I keep getting this error... Optional(["error_message": This IP, site or mobile application is not authorized to use this API key. Request received from IP address 2607:f598:b349:fd01:94f8:7386:d82c:1b29, with

Crash when accessing Object properties after decoding from JSON

扶醉桌前 提交于 2020-01-04 15:57:12
问题 I have decodes JSON value into object. The object looks as expected, but when I try to access it's property. let jsonData = try JSONSerialization.data(withJSONObject: JSON, options: []) let decoder = JSONDecoder() let doctor = try! decoder.decode(Doctor.self, from: jsonData) let txt = "\(doctor.title). \(doctor.firstName) \(doctor.lastName)" // Runtime crash: (Thread 1: EXC_BAD_ACCESS (code=1, address=0x40)) Runtime crash: (Thread 1: EXC_BAD_ACCESS (code=1, address=0x40)) Class Person: import