ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Ruby Flavor in Swift
write your own
pseudo syntax sugar
2015/02/15
Swift Developers MeetUp in Tokyo
Hisakuni Fujimoto
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
Syntax sugar
of
closure argument
queue.addOperationWithBlock({
...
})
Inside of ¡õ is a closure argument.
Base Syntax of Swift
Passing closure argument in parentheses
like Objective-C, JavaScript and etc.
queue.addOperationWithBlock() {
...
}
Syntax Sugar of Swift
Closure argument go out parentheses
of argument list.
Syntax Sugar of Swift
When other argument is nothing,
omission of parentheses is possible.
queue.addOperationWithBlock {
...
}
The person who proposed
this syntax surely likes Ruby.
Because..
block syntax of Ruby
queue.addOperationWithBlock do
...
end
queue.addOperationWithBlock {
...
}
or
block syntax of Ruby
queue.addOperationWithBlock {
...
}
ÍêÈ«¤ËÒ»ÖÂ
Completely Match
Write your own
pseudo syntax sugar
In Ruby on Rails app,
Pod?le of CocoaPods
and etc,block syntax of
Ruby is used very well.
We can do same
thing using Swift.
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
Pseudo Syntax Sugar
delegate method invocation
ifRespondTo("cameraPickerDidCancel:") {
$0.cameraPickerDidCancel!(self)
}
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
Pseudo Syntax Sugar (again)
delegate method invocation
ifRespondTo("cameraPickerDidCancel:") {
$0.cameraPickerDidCancel!(self)
}
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!
2015/02/15
Swift Developers MeetUp in Tokyo
Hisakuni Fujimoto
Thank You!

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
  • 4. queue.addOperationWithBlock({ ... }) Inside of ¡õ is a closure argument. Base Syntax of Swift Passing closure argument in parentheses like Objective-C, JavaScript and etc.
  • 5. queue.addOperationWithBlock() { ... } Syntax Sugar of Swift Closure argument go out parentheses of argument list.
  • 6. Syntax Sugar of Swift When other argument is nothing, omission of parentheses is possible. queue.addOperationWithBlock { ... }
  • 7. The person who proposed this syntax surely likes Ruby. Because..
  • 8. block syntax of Ruby queue.addOperationWithBlock do ... end queue.addOperationWithBlock { ... } or
  • 9. block syntax of Ruby queue.addOperationWithBlock { ... } ÍêÈ«¤ËÒ»Ö Completely Match
  • 10. Write your own pseudo syntax sugar
  • 11. In Ruby on Rails app, Pod?le of CocoaPods and etc,block syntax of Ruby is used very well.
  • 12. We can do same thing using Swift.
  • 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
  • 14. Pseudo Syntax Sugar delegate method invocation ifRespondTo("cameraPickerDidCancel:") { $0.cameraPickerDidCancel!(self) }
  • 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
  • 16. Pseudo Syntax Sugar (again) delegate method invocation ifRespondTo("cameraPickerDidCancel:") { $0.cameraPickerDidCancel!(self) }
  • 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!
  • 18. 2015/02/15 Swift Developers MeetUp in Tokyo Hisakuni Fujimoto Thank You!