Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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 (野良プログラマ)
• RubyCoco...
Syntax sugar
of
closure argument
queue.addOperationWithBlock({
...
})
Inside of □ is a closure argument.
Base Syntax of Swift
Passing closure argument in p...
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,
Podfile 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().addOp...
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.r...
Pseudo Syntax Sugar (again)
delegate method invocation
ifRespondTo("cameraPickerDidCancel:") {
$0.cameraPickerDidCancel!(s...
Summary
1. Swift has closure syntax sugar similar to the
block syntax of Ruby.
2. Make your pseudo syntax sugar using
clos...
2015/02/15
Swift Developers MeetUp in Tokyo
Hisakuni Fujimoto
Thank You!
Upcoming SlideShare
Loading in …5
×

Ruby Flavor in Swift (3min)

334 views

Published on

3 minutes introduction about Ruby flavor in Swift. The theme is "write your own pseudo syntactic sugar."

Published in: Software
  • Be the first to comment

  • Be the first to like this

Ruby Flavor in Swift (3min)

  1. 1. Ruby Flavor in Swift write your own pseudo syntax sugar 2015/02/15 Swift Developers MeetUp in Tokyo Hisakuni Fujimoto
  2. 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
  3. 3. Syntax sugar of closure argument
  4. 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. 5. queue.addOperationWithBlock() { ... } Syntax Sugar of Swift Closure argument go out parentheses of argument list.
  6. 6. Syntax Sugar of Swift When other argument is nothing, omission of parentheses is possible. queue.addOperationWithBlock { ... }
  7. 7. The person who proposed this syntax surely likes Ruby. Because..
  8. 8. block syntax of Ruby queue.addOperationWithBlock do ... end queue.addOperationWithBlock { ... } or
  9. 9. block syntax of Ruby queue.addOperationWithBlock { ... } 完全に一致 Completely Match
  10. 10. Write your own pseudo syntax sugar
  11. 11. In Ruby on Rails app, Podfile of CocoaPods and etc,block syntax of Ruby is used very well.
  12. 12. We can do same thing using Swift.
  13. 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. 14. Pseudo Syntax Sugar delegate method invocation ifRespondTo("cameraPickerDidCancel:") { $0.cameraPickerDidCancel!(self) }
  15. 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. 16. Pseudo Syntax Sugar (again) delegate method invocation ifRespondTo("cameraPickerDidCancel:") { $0.cameraPickerDidCancel!(self) }
  17. 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. 18. 2015/02/15 Swift Developers MeetUp in Tokyo Hisakuni Fujimoto Thank You!

×