From Ruby to Objective-C

F2dcf6633971844e19ca96ea294ba976?s=47 高見龍
November 27, 2013

From Ruby to Objective-C

because Ruby and Objective-C has the same ancestor, they have many things in common, and in this talk I mainly talk about what I learned in Ruby, borrow those experiences and move to Objective-C :)

Ruby Tuesday #27 @Taiwan

F2dcf6633971844e19ca96ea294ba976?s=128

高見龍

November 27, 2013
Tweet

Transcript

  1. None
  2. from Ruby to Objective-C

  3. None
  4. I’m a Ruby guy (≈ 5 years) I’m a iOS

    app guy (≈ 3 years) I’m a Flash guy (≈ 9 years) I’m a Python guy (≈ 3 years)
  5. Ruby > Rails

  6. Current Status 80% iOS app, 20% Ruby/Rails

  7. 100% Ruby Lover!

  8. Rails Girls Taipei

  9. Rails Girls Taipei

  10. WebConf Taiwan 2014

  11. Today, I’m NOT talking about.. how to use Ruby to

    write iOS app!
  12. I’m going to talk about..

  13. what I learned in Ruby…

  14. and move to Objective-C

  15. after all, our life, time and resources are limited

  16. None
  17. what about Objective-C?

  18. “it has god dame long method name and weird parameters!”

    Objective-C … NSArray* languages = [NSArray arrayWithObjects:@"Ruby", @"PHP", @"Objective-C", nil]; ! [languages enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"language = %@", obj); }];
  19. “what the hell is the square bracket!” Objective-C … NSString*

    myName = @"eddie kao"; NSLog(@"%@", [myName uppercaseString]);
  20. “WTF! my app crashed again!!” Objective-C …

  21. Introduction

  22. Ruby was born on 1993 Objective-C was born on 1983

  23. they have the same ancestor

  24. Smalltalk photo by Marcin Wichary

  25. Ruby is general-purpose Objective-C mainly used in Mac/iOS app development

  26. Ruby != Rails Objective-C != Cocoa Framework

  27. they have something in common..

  28. both Ruby and Objective-C are Object-Oriented

  29. both Ruby and Objective-C are strongly typed language.

  30. Ruby is a dynamic language Objective-C is a dynamic language

  31. both Ruby and Objective-C are Dynamic Typing

  32. Type checking.. - (void) makeSomeNoise:(id) sender { if ([sender isKindOfClass:[RobberDuck

    class]]) { RobberDuck* duck = (RobberDuck *) sender; [duck quack]; } }
  33. or you can do this.. - (void) makeSomeNoise:(id) sender {

    if ([sender respondsToSelector:@selector(quack)]) { [sender quack]; } }
  34. None
  35. id

  36. Objective-C is superset of C

  37. Objective-C is still C

  38. NSString, NSArray, NSNumber…

  39. NextSTEP

  40. CF… = Core Foundation CG… = Core Graphic CL… =

    Core Location CA… = Core Animation UI… = User Interface
  41. OOP

  42. everything in Ruby is an object…

  43. and almost everything in Objective-C is an objects..

  44. there’re still some primitive data types in Objective-C

  45. object model class Animal end ! class Dog < Animal

    end
  46. object model dog = Dog.new ! puts "class of dog

    is #{dog.class}” # Dog puts "superclass of dog is #{dog.class.superclass}” # Animal puts "super superclass of dog is #{dog.class.superclass.superclass}” # Object puts "super super superclass of dog is #{dog.class.superclass.superclass.superclass}” # BasicObject ! puts "class of Dog is #{Dog.class}” # Class puts "class class of Dog is #{Dog.class.class}” # Class puts "class of Animal is #{Animal.class}” # Class puts "class of Object is #{Object.class}” # Class
  47. object model @interface Animal : NSObject @end ! @implementation Animal

    @end ! @interface Dog : Animal @end ! @implementation Dog @end
  48. object model Dog* dog = [[Dog alloc] init]; ! NSLog(@"class

    of dog is %@", [dog class]); # Dog ! NSLog(@"superclass of dog is %@", [dog superclass]); # Animal ! NSLog(@"super superclass of dog is %@", [[dog superclass] superclass]); # NSObject ! NSLog(@"super super superclass of dog is %@", [[[dog superclass] superclass] superclass]); # null
  49. Object Model reference: http://goo.gl/wYL6gT

  50. method & message

  51. method definition - (void) sayHello:(id)someOne withMessage:(NSString *)message { NSLog(@"Hello %@,

    %@", someOne, message); } def say_hello(someone, message) puts "Hello #{someone}, #{message}" end
  52. sending message [dog walk]; dog.walk() # or you can omit

    the parentheses
  53. [fox saySomething:@"hi, Ruby"]; fox.say_something "hi, Ruby" # what does the

    fox say? sending message
  54. sending message puts 1 + 2 puts 1.+(2) puts 1.send(:+,

    2)
  55. sending message class Bank def save(money) puts "you just saved

    #{money} dollars" end end ! bank = Bank.new bank.save 20 # you just saved 20 dollars bank.send(:save, 20) # you just saved 20 dollars
  56. sending message @interface Bank : NSObject - (void) save:(NSNumber *)

    money; @end ! @implementation Bank - (void)save:(NSNumber *)money { NSLog(@"you just saved %@ dollars", money); } @end ! Bank* bank = [[Bank alloc] init]; [bank save:@20]; [bank performSelector:@selector(save:) withObject:@20];
  57. block

  58. block p1 = Proc.new { puts "Hello, Proc Block" }

    p1.call ! p2 = lambda { puts "Hello, Lambda Block" } p2.call
  59. block ^{ };

  60. block typedef void (^MyBlock)(void); int age = 18; MyBlock theBlock

    = ^{ NSLog(@"Hello, Objective-C Block, your age = %d", age); }; ! theBlock(); # Hello, Objective-C Block, your age = 18 ! age = 38; theBlock(); # guess what’s the age?
  61. block 3.times { |i| puts i } NSArray* list =

    @[@1, @2, @3]; [list enumerateObjectsUsingBlock:^(NSNumber* num, NSUInteger idx, BOOL *stop) { NSLog(@"%@", num); }];
  62. iteration

  63. iteration list = [1, 2, 3, 4, 5] ! sum

    = 0 ! list.each { |num| sum += num } ! puts "sum = #{sum}"
  64. iteration NSArray* list = @[@1, @2, @3, @4, @5]; !

    __block int sum = 0; ! [list enumerateObjectsUsingBlock:^(NSNumber* num, NSUInteger idx, BOOL *stop) { sum += [num intValue]; }]; ! NSLog(@"sum = %d", sum);
  65. iteration class Fox def say puts "what does the fox

    say?" end end ! fox1 = Fox.new fox2 = Fox.new fox3 = Fox.new foxes = [fox1, fox2, fox3] ! foxes.map { |fox| fox.say } # what does the fox say?
  66. iteration @interface Fox : NSObject - (void) say; @end !

    @implementation Fox - (void) say { NSLog(@"what does the fox say?!"); } @end
  67. iteration Fox* fox1 = [[Fox alloc] init]; Fox* fox2 =

    [[Fox alloc] init]; Fox* fox3 = [[Fox alloc] init]; ! NSArray* foxes = @[fox1, fox2, fox3]; ! [foxes makeObjectsPerformSelector:@selector(say)];
  68. add methods at runtime

  69. Open class class String def is_awesome? return true if self

    == "Ruby Tuesday" end end ! puts "Ruby Tuesday".is_awesome?
  70. Category @interface NSString(RubyTuesday) - (BOOL) isAwesome; @end ! @implementation NSString(RubyTuesday)

    - (BOOL) isAwesome { if ([self isEqualToString:@"Ruby Tuesday"]){ return YES; } return NO; } @end
  71. Category NSString* meetup = @"Ruby Tuesday"; if ([meetup isAwesome]) {

    NSLog(@"AWESOME!"); }
  72. <objc/runtime.h> Working with Classes class_getName class_getSuperclass class_getInstanceVariable class_getClassVariable class_addIvar class_copyIvarList

    class_addMethod class_getInstanceMethod class_getClassMethod class_replaceMethod class_respondsToSelector .. reference: http://goo.gl/BEikIM
  73. Working with Instances object_copy object_dispose object_setInstanceVariable object_getInstanceVariable object_getIndexedIvars object_getIvar object_setIvar

    object_getClassName object_getClass object_setClass .. reference: http://goo.gl/BEikIM <objc/runtime.h>
  74. reflection - (BOOL) isKindOfClass:(Class) aClass - (BOOL) isMemberOfClass:(Class) aClass -

    (BOOL) respondsToSelector:(SEL) aSelector - (BOOL) conformsToProtocol:(Protocol *) aProtocol .. reference: http://goo.gl/fgmJcg
  75. ecosystem

  76. open source projects on Github Ruby : 76,574 Objective-C :

    22,959
  77. Ruby : bundler source 'https://rubygems.org' ! gem 'rails', '3.2.8' gem

    'mysql2' ! group :assets do gem 'sass-rails', '~> 3.2.3' gem "bootstrap-sass" end ! gem "kaminari" gem "simple_form" gem "carrierwave" gem 'unicorn'
  78. Objective-C : cocoapods platform :ios, '6.0' ! pod 'Facebook-iOS-SDK', '~>

    3.5.1’ pod 'JSONKit', '~> 1.5pre' pod 'MagicalRecord', '~> 2.0.7’ pod 'SSKeychain', '~> 0.1.4’ pod 'TestFlightSDK', '~> 1.1' pod 'SMCalloutView', '~> 1.1.2' ! target :UnitTests do link_with 'UnitTests' pod 'OCMock', '~> 2.0.1' pod 'OCHamcrest', '~> 1.9' end
  79. IMHO

  80. Objective-C is not really hard to learn…

  81. the actual difficult part in iOS app development is Cocoa

    Framework
  82. Objective-C would be almost useless without Cocoa Framework

  83. Ruby without Rails?!

  84. design patterns observer singleton delegation command target-action composite notifications proxy

    MVC
  85. C

  86. what else..

  87. photoed by JD Hancock

  88. None
  89. reference: http://goo.gl/2mzyMY Flash Display Hierarchy

  90. reference: http://goo.gl/xhS7m7 UIKit and AppKit framework Hierarchy

  91. Views reference: http://goo.gl/xhS7m7

  92. reference: http://goo.gl/xhS7m7 Views

  93. and I read Ruby source code..

  94. my iOS app dev experience = Ruby + C +

    Flash/AS3 = +
  95. ⾼高⾒見⻯⿓龍 Contacts photo by Eddie Website Blog Plurk Facebook Google

    Plus Twitter Email Mobile http://www.eddie.com.tw http://blog.eddie.com.tw http://www.plurk.com/aquarianboy http://www.facebook.com/eddiekao http://www.eddie.com.tw/+ https://twitter.com/#!/eddiekao eddie@digik.com.tw +886-928-617-687