Difference between revisions of "Swift code snippets"

From Hawk Wiki
Jump to: navigation, search
(Created page with "==Making Network Request== <pre> var clientId = "Put your client id here" var url = NSURL(string: "https://api.instagram.com/v1/media/popular?client_id=\(clientId)")! var reque...")
 
Line 12: Line 12:
 
     println("response: \(self.photos)")
 
     println("response: \(self.photos)")
 
}
 
}
 +
</pre>
 +
==Use setImageWithURL==
 +
Create the bridging file: create a new Objective-C file, name it anything, and when you save it, Xcode will prompt you generate a bridging file. <br>
 +
In an Objective-C bridging file:
 +
<pre>
 +
#import <AFNetworking/UIImageView+AFNetworking.h>
 +
</pre>
 +
In the swift view file, to set a imageView using URL:
 +
<pre>
 +
setImageWithURL(NSURL(string: thumbnail))
 
</pre>
 
</pre>

Revision as of 06:33, 6 February 2015

Making Network Request

var clientId = "Put your client id here"
 
var url = NSURL(string: "https://api.instagram.com/v1/media/popular?client_id=\(clientId)")!
var request = NSURLRequest(URL: url)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
    var responseDictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as NSDictionary
    self.photos = responseDictionary["data"] as NSArray
    self.tableView.reloadData()
    
    println("response: \(self.photos)")
}

Use setImageWithURL

Create the bridging file: create a new Objective-C file, name it anything, and when you save it, Xcode will prompt you generate a bridging file.
In an Objective-C bridging file:

#import <AFNetworking/UIImageView+AFNetworking.h>

In the swift view file, to set a imageView using URL:

setImageWithURL(NSURL(string: thumbnail))