title: Swift - Write Image from URL to Local File
body: <p>I've been learning swift rather quickly, and I'm trying to develop an OS X application that downloads images.</p>
  
  <p>I've been able to parse the JSON I'm looking for into an array of URLs as follows:</p>
  
  <pre><code>func didReceiveAPIResults(results: NSArray) {
      println(results)
      for link in results {
          let stringLink = link as String
          //Check to make sure that the string is actually pointing to a file
          if stringLink.lowercaseString.rangeOfString(".jpg") != nil {2
  
              //Convert string to url
              var imgURL: NSURL = NSURL(string: stringLink)!
  
              //Download an NSData representation of the image from URL
              var request: NSURLRequest = NSURLRequest(URL: imgURL)
  
              var urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)!
              //Make request to download URL
              NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: { (response: NSURLResponse!, data: NSData!, error: NSError!) -&gt; Void in
                  if !(error? != nil) {
                      //set image to requested resource
                      var image = NSImage(data: data)
  
                  } else {
                      //If request fails...
                      println("error: \(error.localizedDescription)")
                  }
              })
          }
      }
  }
  </code></pre>
  
  <p>So at this point I have my images defined as "image", but what I'm failing to grasp here is how to save these files to my local directory.</p>
  
  <p>Any help on this matter would be greatly appreciated!</p>
  
  <p>Thanks,</p>
  
  <p>tvick47</p>