Comment

Fetching data with NSURLSession

It turns out that getting some file from the internet is relatively easy with the new NSURLSession API.

A simple example:

brush: swift
let defaultSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
var dataTask: NSURLSessionDataTask?

func fetchData(sender: AnyObject) {
    if dataTask != nil { dataTask?.cancel() }
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

    let url = NSURL(string: "http://localhost:8000/a.json")

    dataTask = defaultSession.dataTaskWithURL(url!) {
        data, response, error in
        dispatch_async(dispatch_get_main_queue()) {
        UIApplication.sharedApplication().networkActivityIndicatorVisible =   false
        }

        if let error = error {
            print(error.localizedDescription)
        } 
        else if let httpResponse = response as? NSHTTPURLResponse {
            if httpResponse.statusCode == 200 {
                self.updateData(data)
            }
        }
    }
    dataTask?.resume()
}

By the way, it turns out that fetching simple http is no longer the advised way. Only https is accepted out of the box. To allow http for testing, you need to add the following to the Info.plist:

  • App Transport Security Settings
    • Allow Arbitrary Loads: YES

Based on this article

Comment

Comment

Parsing JSON in Swift

Parsing JSON files is easy as long as you remember that all the Data is of type AnyObject and that you can try to cast the fields to String or Int as needed. An array is of type [AnyObject], a dictionary of type [String: AnyObject], a string of type String and an integer of type Int.

We will write some code to parse this very simple JSON file:

{
  "people": [ "bob", "rob"]
}

The code to parse this is:

brush: swift
  func parseJSON(data: NSData?) {
    do {
      if let data = data, response = try NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions(rawValue:0)) as? [String: AnyObject] {   
        // people is an array
        if let array: AnyObject = response["people"] {
          for person in array as! [AnyObject] {
            if let name = person as? String {
              // Do something with the name
              postNameInObscureBlog(name)
            } else {
              print("Not a string")
            }
          }
        } 
        else {
          print("people key not found in dictionary")
        }
      }
      else {
        print("JSON Error")
      }
    } catch let error as NSError {
      print("Error parsing results: \(error.localizedDescription)")
    }
  }

Comment

Comment

Swift closures

What a great invention! With closures you can put the code where together where it belongs and there is no need to pepper your code with little helper functions.

Here are some things you can do with closures:

brush: swift
func loadData(completion : (result : Int, success : Bool) -> (Void)) {
  completion(3, true)
}

loadData() {
   result, success in

   if success {
     print("Success! The data was \(result)"
   }
   else {
   print("Failure!")
}

Comment