-1

I'm brand new to Swift development. I'm trying to build a basic app which will read a feed from Google Calendar which is returned in JSON.

I am able to use a NSURLConnection.sendAsynchronousRequest call as described in this thread: Make REST API call in Swift and I get a result of some sorts.

My problem is I am unable to parse the JSON into some meaningful objects from which I can create a list of upcoming events.

My code this far is:

var url : String = "some url"
var request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
    var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
    let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary

    if (jsonResult != nil){
      // No idea how to parse this object as it seems to be a NSDictionary but battling to interact with it.
    }
    else 
    {

    }
})

Any help would be greatly appreciated.

TResponse
  • 3,940
  • 7
  • 43
  • 63
  • 1
    You should not use Calendar GData feeds. GData shutdown date is Nov 17th this year. Take a look into the v3 version instead: https://developers.google.com/google-apps/calendar/ – luc Nov 08 '14 at 21:09
  • Thanks a lot for that - very helpful.. I did not know they were ending it. – TResponse Nov 09 '14 at 02:20

1 Answers1

1

If you put a breakpoint after

let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary

then you'll be able to see the dictionary key/value pairs. I copied/pasted your code and see this:

Debugger

You can access the values by using jsonResult.objectForKey("keyName"). I'm also going to downcast using as DataType.

Example)

let encoding = jsonResult.objectForKey("encoding") as String
let version = jsonResult.objectForKey("version") as String
let feed = jsonResult.objectForKey("feed") as NSDictionary
Ron Fessler
  • 2,701
  • 1
  • 17
  • 22
  • jsonResult.objectForKey("keyName") was they key here for me and what I was missing. I found that I was trying to read the objects in an incorrect way. – TResponse Nov 06 '14 at 20:34