Swift from Objective for UI
Contents
animateWithDuration
UIView.animateWithDuration(Double(1.0), animations: {() -> Void in }, completion: {(Bool) -> Void in })
NSTimer
NSTimer.scheduledTimerWithTimeInterval(Double(10.0), target: self, selector: "timerDone", userInfo: nil, repeats: false)
required init(coder aDecoder: NSCoder)
required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }
Selector
We cannot use @selector in swift.
We use String style “method:” in swift
NSNotificationCenter.defaultCenter().addObserver(self, selector: "update:", name:"Update", object:nil)
Or you can use Selector(“”)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("update:"), name:"Update", object:nil)
Method style is same as Objective-C
dealloc to deinit
dealloc is invoked when disposing object.
In swift, we use deinit
deinit { // dispose delegate, notification etc... }
deinit is a kind of destructor
Event and sender
Use AnyObject instead of id
func click(sender : AnyObject) { // You can convert any type in here }