from Ruby to Objective-C

95

description

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

Transcript of from Ruby to Objective-C

Page 1: from Ruby to Objective-C
Page 2: from Ruby to Objective-C

from Ruby to Objective-C

Page 3: from Ruby to Objective-C
Page 4: from Ruby to Objective-C

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)

Page 5: from Ruby to Objective-C

Ruby > Rails

Page 6: from Ruby to Objective-C

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

Page 7: from Ruby to Objective-C

100% Ruby Lover!

Page 8: from Ruby to Objective-C

Rails Girls Taipei

Page 9: from Ruby to Objective-C

Rails Girls Taipei

Page 10: from Ruby to Objective-C

WebConf Taiwan 2014

Page 11: from Ruby to Objective-C

Today, I’m NOT talking about..

how to use Ruby to write iOS app!

Page 12: from Ruby to Objective-C

I’m going to talk about..

Page 13: from Ruby to Objective-C

what I learned in Ruby…

Page 14: from Ruby to Objective-C

and move to Objective-C

Page 15: from Ruby to Objective-C

after all, our life, time and resources are limited

Page 16: from Ruby to Objective-C
Page 17: from Ruby to Objective-C

what about Objective-C?

Page 18: from Ruby to Objective-C

“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); }];

Page 19: from Ruby to Objective-C

“what the hell is the square bracket!”

Objective-C …

NSString* myName = @"eddie kao"; NSLog(@"%@", [myName uppercaseString]);

Page 20: from Ruby to Objective-C

“WTF! my app crashed again!!”

Objective-C …

Page 21: from Ruby to Objective-C

Introduction

Page 22: from Ruby to Objective-C

Ruby was born on 1993

Objective-C was born on 1983

Page 23: from Ruby to Objective-C

they have the same ancestor

Page 24: from Ruby to Objective-C

Smalltalkphoto by Marcin Wichary

Page 25: from Ruby to Objective-C

Ruby is general-purpose

Objective-C mainly used in Mac/iOS app development

Page 26: from Ruby to Objective-C

Ruby != Rails

Objective-C != Cocoa Framework

Page 27: from Ruby to Objective-C

they have something in common..

Page 28: from Ruby to Objective-C

both Ruby and Objective-C are Object-Oriented

Page 29: from Ruby to Objective-C

both Ruby and Objective-C are strongly typed language.

Page 30: from Ruby to Objective-C

Ruby is a dynamic language

Objective-C is a dynamic language

Page 31: from Ruby to Objective-C

both Ruby and Objective-C are Dynamic Typing

Page 32: from Ruby to Objective-C

Type checking..- (void) makeSomeNoise:(id) sender { if ([sender isKindOfClass:[RobberDuck class]]) { RobberDuck* duck = (RobberDuck *) sender; [duck quack]; } }

Page 33: from Ruby to Objective-C

or you can do this..- (void) makeSomeNoise:(id) sender { if ([sender respondsToSelector:@selector(quack)]) { [sender quack]; } }

Page 34: from Ruby to Objective-C
Page 35: from Ruby to Objective-C

id

Page 36: from Ruby to Objective-C

Objective-C is superset of C

Page 37: from Ruby to Objective-C

Objective-C is still C

Page 38: from Ruby to Objective-C

NSString, NSArray, NSNumber…

Page 39: from Ruby to Objective-C

NextSTEP

Page 40: from Ruby to Objective-C

CF… = Core Foundation CG… = Core Graphic CL… = Core Location

CA… = Core Animation UI… = User Interface

Page 41: from Ruby to Objective-C

OOP

Page 42: from Ruby to Objective-C

everything in Ruby is an object…

Page 43: from Ruby to Objective-C

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

Page 44: from Ruby to Objective-C

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

Page 45: from Ruby to Objective-C

object modelclass Animal end !class Dog < Animal end

Page 46: from Ruby to Objective-C

object modeldog = 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

Page 47: from Ruby to Objective-C

object model@interface Animal : NSObject @end !@implementation Animal @end !@interface Dog : Animal @end !@implementation Dog @end

Page 48: from Ruby to Objective-C

object modelDog* 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

Page 49: from Ruby to Objective-C

Object Modelreference: http://goo.gl/wYL6gT

Page 50: from Ruby to Objective-C

method & message

Page 51: from Ruby to Objective-C

method definition

- (void) sayHello:(id)someOne withMessage:(NSString *)message { NSLog(@"Hello %@, %@", someOne, message); }

def say_hello(someone, message) puts "Hello #{someone}, #{message}" end

Page 52: from Ruby to Objective-C

sending message

[dog walk];

dog.walk() # or you can omit the parentheses

Page 53: from Ruby to Objective-C

[fox saySomething:@"hi, Ruby"];

fox.say_something "hi, Ruby" # what does the fox say?

sending message

Page 54: from Ruby to Objective-C

sending message

puts 1 + 2

puts 1.+(2)

puts 1.send(:+, 2)

Page 55: from Ruby to Objective-C

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

Page 56: from Ruby to Objective-C

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];

Page 57: from Ruby to Objective-C

block

Page 58: from Ruby to Objective-C

block

p1 = Proc.new { puts "Hello, Proc Block" } p1.call !p2 = lambda { puts "Hello, Lambda Block" } p2.call

Page 59: from Ruby to Objective-C

block

^{ };

Page 60: from Ruby to Objective-C

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?

Page 61: from Ruby to Objective-C

block

3.times { |i| puts i }

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

Page 62: from Ruby to Objective-C

iteration

Page 63: from Ruby to Objective-C

iteration

list = [1, 2, 3, 4, 5] !sum = 0 !list.each { |num| sum += num } !puts "sum = #{sum}"

Page 64: from Ruby to Objective-C

iterationNSArray* 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);

Page 65: from Ruby to Objective-C

iterationclass 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?

Page 66: from Ruby to Objective-C

iteration@interface Fox : NSObject - (void) say; @end !@implementation Fox - (void) say { NSLog(@"what does the fox say?!"); } @end

Page 67: from Ruby to Objective-C

iterationFox* fox1 = [[Fox alloc] init]; Fox* fox2 = [[Fox alloc] init]; Fox* fox3 = [[Fox alloc] init]; !NSArray* foxes = @[fox1, fox2, fox3]; ![foxes makeObjectsPerformSelector:@selector(say)];

Page 68: from Ruby to Objective-C

add methods at runtime

Page 69: from Ruby to Objective-C

Open class

class String def is_awesome? return true if self == "Ruby Tuesday" end end !puts "Ruby Tuesday".is_awesome?

Page 70: from Ruby to Objective-C

Category

@interface NSString(RubyTuesday) - (BOOL) isAwesome; @end !@implementation NSString(RubyTuesday) - (BOOL) isAwesome { if ([self isEqualToString:@"Ruby Tuesday"]){ return YES; } return NO; } @end

Page 71: from Ruby to Objective-C

Category

NSString* meetup = @"Ruby Tuesday"; if ([meetup isAwesome]) { NSLog(@"AWESOME!"); }

Page 72: from Ruby to Objective-C

<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

Page 73: from Ruby to Objective-C

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>

Page 74: from Ruby to Objective-C

reflection- (BOOL) isKindOfClass:(Class) aClass - (BOOL) isMemberOfClass:(Class) aClass - (BOOL) respondsToSelector:(SEL) aSelector - (BOOL) conformsToProtocol:(Protocol *) aProtocol ..

reference: http://goo.gl/fgmJcg

Page 75: from Ruby to Objective-C

ecosystem

Page 76: from Ruby to Objective-C

open source projects on Github

Ruby : 76,574

Objective-C : 22,959

Page 77: from Ruby to Objective-C

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'

Page 78: from Ruby to Objective-C

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

Page 79: from Ruby to Objective-C

IMHO

Page 80: from Ruby to Objective-C

Objective-C is not really hard to learn…

Page 81: from Ruby to Objective-C

the actual difficult part in iOS app development is Cocoa Framework

Page 82: from Ruby to Objective-C

Objective-C would be almost useless without Cocoa Framework

Page 83: from Ruby to Objective-C

Ruby without Rails?!

Page 84: from Ruby to Objective-C

design patterns

observersingleton

delegationcommand

target-action

composite

notifications

proxy

MVC

Page 85: from Ruby to Objective-C

C

Page 86: from Ruby to Objective-C

what else..

Page 87: from Ruby to Objective-C

photoed by JD Hancock

Page 88: from Ruby to Objective-C
Page 89: from Ruby to Objective-C

reference: http://goo.gl/2mzyMY

Flash Display Hierarchy

Page 90: from Ruby to Objective-C

reference: http://goo.gl/xhS7m7

UIKit and AppKit framework Hierarchy

Page 91: from Ruby to Objective-C

Viewsreference: http://goo.gl/xhS7m7

Page 92: from Ruby to Objective-C

reference: http://goo.gl/xhS7m7

Views

Page 93: from Ruby to Objective-C

and I read Ruby source code..

Page 94: from Ruby to Objective-C

my iOS app dev experience = Ruby + C + Flash/AS3

= +

Page 95: from Ruby to Objective-C

⾼高⾒見⻯⿓龍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

[email protected]

+886-928-617-687