Difference between revisions of "Swift code snippets"

From Hawk Wiki
Jump to: navigation, search
(Navigate to Storyboard ID)
 
(27 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Swift Code snippts=
+
=Swift Code Snippts=
 
Back To [[IOS_Swift]]
 
Back To [[IOS_Swift]]
==Making Network Request==
+
==[[Swift: Use NSUserDefaults to Store Persistence Data]]==
<pre class="brush:swift">
+
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)")
+
}
+
</pre>
+
  
==Use AFNetworking setImageWithURL==
+
==[[Swift: Making Network Request Using NSURLConnection]]==
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 class="brush:swift">
+
#import <AFNetworking/UIImageView+AFNetworking.h>
+
</pre>
+
In the swift view file, to set a imageView using URL:
+
<pre class="brush:swift">
+
setImageWithURL(NSURL(string: thumbnailURL))
+
</pre>
+
  
==Adding UIRefreshControl==
+
==[[Swift:Use AFNetworking setImageWithURL]]==
<pre class="brush:swift">
+
//refreshControl is automatically defined in UITableViewController
+
var refreshControl: UIRefreshControl?
+
override func viewDidLoad() {
+
    super.viewDidLoad()
+
   
+
    refreshControl = UIRefreshControl()
+
    refreshControl?.addTarget(self, action: "fetch", forControlEvents: UIControlEvents.ValueChanged)
+
    tableView.insertSubview(refreshControl!, atIndex: 0)
+
}
+
  
//to stop refresh, call
+
==[[Swift:Using UIRefreshControl]]==
if let rc = self.refreshControl {
+
    self.refreshControl!.endRefreshing()
+
}
+
</pre>
+
  
==Example of fetching messages from Parse==
+
==[[Swift:Using UIGestureRecognizer]]==
 +
 
 +
==Swift:Example of fetching messages from Parse==
 
https://gist.github.com/sandofsky/7134b1ff90d235901254
 
https://gist.github.com/sandofsky/7134b1ff90d235901254
==Auto Table Row Height==
+
 
<pre class="brush:swift">
+
==[[Swift:Auto Table Row Height]]==
//Auto table row height
+
 
tableView.estimatedRowHeight = 92.0
+
==[[Swift:Navigation between storyboards]]==
tableView.rowHeight = UITableViewAutomaticDimension
+
 
</pre>
+
==[[Swift:Notification Observer Using NSNotificationCenter]]==
==Navigation between storyboards==
+
 
===Navigate to Storyboard ID===
+
==[[Swift:Get Raw Histogram from CGImage]]==
<pre class="brush:swift">
+
 
var vc = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as UIViewController
+
==[[Swift:UIGraphics Drawing on UIView]]==
window?.rootViewController = vc
+
 
</pre>
+
==[[Swift:UIGraphics Scale Down CGImage]]==
===Navigate Segue by Identifier===
+
 
First, drag a segue in storyboard and give it an identifier, then in the button action (or any action you like), do:
+
==[[Swift: Convert between CGImage, CIImage and UIImage]]==
<pre class="brush:swift">
+
 
self.performSegueWithIdentifier("loginSegue", sender: self)
+
==[[Swift:Detect Volumn Button Press]]==
</pre>
+
 
===Get UIVewController from Navigation Controller===
+
==[[Swift:NSTimer scheduledTimerWithTimeInterval]]==
Sometimes, you want to access the UIViewController from a navigation controller
+
<pre class="brush:swift">
+
var vc = storyboard?.instantiateViewControllerWithIdentifier("navigationID") as UINavigationController
+
var v = vc.viewControllers[0] as ViewController // [0] represents the first level UIViewController under NavigationController
+
</pre>
+

Latest revision as of 03:10, 9 April 2015

Swift Code Snippts

Back To IOS_Swift

Swift: Use NSUserDefaults to Store Persistence Data

Swift: Making Network Request Using NSURLConnection

Swift:Use AFNetworking setImageWithURL

Swift:Using UIRefreshControl

Swift:Using UIGestureRecognizer

Swift:Example of fetching messages from Parse

https://gist.github.com/sandofsky/7134b1ff90d235901254

Swift:Auto Table Row Height

Swift:Navigation between storyboards

Swift:Notification Observer Using NSNotificationCenter

Swift:Get Raw Histogram from CGImage

Swift:UIGraphics Drawing on UIView

Swift:UIGraphics Scale Down CGImage

Swift: Convert between CGImage, CIImage and UIImage

Swift:Detect Volumn Button Press

Swift:NSTimer scheduledTimerWithTimeInterval