3 minutes introduction about Ruby flavor in Swift. The theme is "write your own pseudo syntactic sugar."
1 of 18
Download to read offline
More Related Content
Ruby Flavor in Swift (3min)
1. Ruby Flavor in Swift
write your own
pseudo syntax sugar
2015/02/15
Swift Developers MeetUp in Tokyo
Hisakuni Fujimoto
2. Self-Introduction
? Hisakuni Fujimoto / ÌÙ±¾Éаî
? @fhisa, https://github.com/hisa
? Freelance Programmer (Ò°Á¼¥×¥í¥°¥é¥Þ)
? RubyCocoa.framework creator
? It had been installed in Mac OS X from 10.5 to 10.9.
? iOS developer of half year experience
13. if let delegate = delegate {
if delegate.respondsToSelector("cameraPickerDidCancel:") {
NSOperationQueue.mainQueue().addOperationWithBlock {
delegate.cameraPickerDidCancel!(self)
}
}
}
Normally
delegate method invocation
? validate the delegate property
? test responsibility
? (wrap in main thread if need)
? invoke the method
15. func ifRespondTo(selector: Selector, handler:CameraPickerDelegate->Void) {
if let delegate = self.delegate {
if delegate.respondsToSelector(selector) {
NSOperationQueue.mainQueue().addOperationWithBlock {
handler(delegate)
}
}
}
}
Implementation of pseudo Syntax Sugar
delegate method invocation
? validate the delegate property
? test responsibility
? (wrap in main thread if need)
? invoke the method in handler
17. Summary
1. Swift has closure syntax sugar similar to the
block syntax of Ruby.
2. Make your pseudo syntax sugar using
closure argument if necessary. To keep
simple your source code!