Slide 1

Slide 1 text

Objective-c FOR JAVA DEVELOPERS

Slide 2

Slide 2 text

1OBjective-c For Java developers The basics

Slide 3

Slide 3 text

1Hello world System.out.println("Hello, World!"); NSLog(@"Hello, World!");

Slide 4

Slide 4 text

1Basic types byte aByte = 0; short aShort = 0; int anInt = 0; long aLong = 0; float aFloat = 0; double aDouble = 0; boolean aBoolean = true; //false char aChar = 'a'; Object anObject = new Object(); char aByte = 0; short aShort = 0; int anInt = 0; long aLong = 0; float aFloat; double aDouble; BOOL aBoolean = YES; //NO char aChar = 'a'; NSObject *anObject = [[NSObject alloc] init];

Slide 5

Slide 5 text

1Methods String aString = new String(chars); NSString *aString = [[NSString alloc] initWithUTF8String:chars];

Slide 6

Slide 6 text

1Methods String aString = new String(chars); NSString *aString = [[NSString alloc] initWithUTF8String:chars];

Slide 7

Slide 7 text

1Methods String aString = new String(chars); NSString *aString = [[NSString alloc] initWithUTF8String:chars];

Slide 8

Slide 8 text

1Methods String aString = new String(chars); NSString *aString = [[NSString alloc] initWithUTF8String:chars];

Slide 9

Slide 9 text

1Methods String aString = new String(chars); NSString *aString = [[NSString alloc] initWithUTF8String:chars];

Slide 10

Slide 10 text

the basics are the same! Recap

Slide 11

Slide 11 text

the basics are the same! also, YAY for unsigned Recap

Slide 12

Slide 12 text

2OBjective-c For Java developers Meet the objects

Slide 13

Slide 13 text

2Pointers String s = new String(); NSString *s = [[NSString alloc] init];

Slide 14

Slide 14 text

2Pointers String s = new String(); NSString *s = [[NSString alloc] init]; Stuff 0x3DE2FE

Slide 15

Slide 15 text

2Pointers String s = new String(); NSString *s = [[NSString alloc] init]; Stuff 0x3DE2FE

Slide 16

Slide 16 text

2Pointers String s = new String(); NSString *s = [[NSString alloc] init]; Stuff 0x3DE2FE

Slide 17

Slide 17 text

2Pointers String s = new String(); NSString *s = [[NSString alloc] init]; Stuff 0x3DE2FE Object *

Slide 18

Slide 18 text

2Pointers String s = new String(); NSString *s = [[NSString alloc] init]; Stuff 0x3DE2FE

Slide 19

Slide 19 text

2NSWhat? "Many of those strange primitive wrapper classes, like Integer and Number came from Lee Boynton, one of the early NeXT Obj-C class library guys who hated 'int' and 'float' types." Patrick Naughton (one of the original creators of Java)

Slide 20

Slide 20 text

2String String aString = new String("A string."); NSString *aString = [[NSString alloc] initWithString:@"A string."];

Slide 21

Slide 21 text

2String String aString = new String("A string."); NSString *aString = [[NSString alloc] initWithString:@"A string."];

Slide 22

Slide 22 text

2String String aString = new String("A string."); NSString *aString = [[NSString alloc] initWithString:@"A string."]; String aString = "A string."; NSString *aString = @"A string.";

Slide 23

Slide 23 text

2“A” + “B” String one = "1 + "; int two = 2; String equals = " = "; float three = 3.0f; String s = one + two + equals + three; NSString *one = @"1 +"; int two = 2; NSString *eq = @"="; float three = 3.0; NSString *s = [NSString stringWithFormat:@"%@ %d %@ %.1f", one, two, eq, three];

Slide 24

Slide 24 text

2“A” + “B” String one = "1 + "; int two = 2; String equals = " = "; float three = 3.0f; String s = one + two + equals + three; NSString *one = @"1 +"; int two = 2; NSString *eq = @"="; float three = 3.0; NSString *s = [NSString stringWithFormat:@"%@ %d %@ %.1f", one, two, eq, three];

Slide 25

Slide 25 text

2“A” + “B” String one = "1 + "; int two = 2; String equals = " = "; float three = 3.0f; String s = one + two + equals + three; NSString *one = @"1 +"; int two = 2; NSString *eq = @"="; float three = 3.0; NSString *s = [NSString stringWithFormat:@"%@ %d %@ %.1f", one, two, eq, three];

Slide 26

Slide 26 text

2“A” + “B” String one = "1 + "; int two = 2; String equals = " = "; float three = 3.0f; String s = one + two + equals + three; NSString *one = @"1 +"; int two = 2; NSString *eq = @"="; float three = 3.0; NSString *s = [NSString stringWithFormat:@"%@ %d %@ %.1f", one, two, eq, three];

Slide 27

Slide 27 text

2“A” + “B” String one = "1 + "; int two = 2; String equals = " = "; float three = 3.0f; String s = one + two + equals + three; NSString *one = @"1 +"; int two = 2; NSString *eq = @"="; float three = 3.0; NSString *s = [NSString stringWithFormat:@"%@ %d %@ %.1f", one, two, eq, three];

Slide 28

Slide 28 text

2“A” + “B” String one = "1 + "; int two = 2; String equals = " = "; float three = 3.0f; String s = one + two + equals + three; NSString *one = @"1 +"; int two = 2; NSString *eq = @"="; float three = 3.0; NSString *s = [NSString stringWithFormat:@"%@ %d %@ %.1f", one, two, eq, three];

Slide 29

Slide 29 text

2“A” + “B” String one = "1 + "; int two = 2; String equals = " = "; float three = 3.0f; String s = one + two + equals + three; NSString *one = @"1 +"; int two = 2; NSString *eq = @"="; float three = 3.0; NSString *s = [NSString stringWithFormat:@"%@ %d %@ %.1f", one, two, eq, three]; But... If I can use C++ then I can use operator overloading to add that to Objective-C, right?

Slide 30

Slide 30 text

2“A” + “B” String one = "1 + "; int two = 2; String equals = " = "; float three = 3.0f; String s = one + two + equals + three; NSString *one = @"1 +"; int two = 2; NSString *eq = @"="; float three = 3.0; NSString *s = [NSString stringWithFormat:@"%@ %d %@ %.1f", one, two, eq, three]; But... If I can use C++ then I can use operator overloading to add that to Objective-C, right? Nop :(

Slide 31

Slide 31 text

2(NS)Array String strings[] = {"1", "2"}; NSArray *strings = @[@"1", @"2"];

Slide 32

Slide 32 text

2(NS)Array String strings[] = new String[10]; NSString **strings = malloc(10*sizeof(NSString *));

Slide 33

Slide 33 text

List strings = new ArrayList(10); NSMutableArray *strings = [NSMutableArray arrayWithCapacity:10]; 2(NS)Array

Slide 34

Slide 34 text

2Dictionary Map m = new HashMap(); m.put("key","value"); NSDictionary *m = @{@"key" : @"value"};

Slide 35

Slide 35 text

2Dictionary Map m = new HashMap(); m.put("key","value"); NSMutableDictionary *m = [@{@"key" : @"value"} mutableCopy]; [m setObject:@"key2" forKey:@"value2"];

Slide 36

Slide 36 text

2Dictionary Map m = new HashMap(); m.put("key","value"); NSMutableDictionary *m = [@{@"key" : @"value"} mutableCopy]; [m setObject:@"key2" forKey:@"value2"];

Slide 37

Slide 37 text

2Dictionary Map m = new HashMap(); m.put("key","value"); NSMutableDictionary *m = [@{@"key" : @"value"} mutableCopy]; [m setObject:@"key2" forKey:@"value2"];

Slide 38

Slide 38 text

2Literals String s = "value1"; Number n = 3.14159265359; String a[] = {"value1","value2"}; Map m = new HashMap(); m.put("key","value"); Number arr[] = { Math.round(2*Math.PI) }; NSString *s = @"value1"; NSNumber *n = @3.14159265359; NSArray *a = @[@"value1", @"value2"]; NSDictionary *m = @{@"key" : @"value"}; NSArray *arr = @[ @(roundf(2*M_PI)) ]; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; dict[@"key1"] = @"value1";

Slide 39

Slide 39 text

Recap POINTER = OBJECT LITERALS ARE AWESOME INIT INITIALIZE ALLOC CREATE

Slide 40

Slide 40 text

Recap POINTER = OBJECT STRING CONCAT SUCKS LITERALS ARE AWESOME INIT INITIALIZE ALLOC CREATE

Slide 41

Slide 41 text

3OBjective-c For Java developers Interfaces & implementations

Slide 42

Slide 42 text

3.h & .m @interface Foo : NSObject { //protected/private/public ivars } //Property declarations //Method declarations @end @implementation Foo { //Private ivars } //Synthesize properties //Method implementations //Private methods @end .h .m

Slide 43

Slide 43 text

3Foo class @interface Foo : NSObject - (id)initWithBar:(NSString *)bar; @end @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { self = [super init]; if (self != nil) { _bar = [bar copy]; } return self; } @end .h .m

Slide 44

Slide 44 text

3 nil? Don’t you mean null? Foo class @interface Foo : NSObject - (id)initWithBar:(NSString *)bar; @end @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { self = [super init]; if (self != nil) { _bar = [bar copy]; } return self; } @end .h .m

Slide 45

Slide 45 text

3 nil? Don’t you mean null? Nop Foo class @interface Foo : NSObject - (id)initWithBar:(NSString *)bar; @end @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { self = [super init]; if (self != nil) { _bar = [bar copy]; } return self; } @end .h .m

Slide 46

Slide 46 text

3 nil? Don’t you mean null? Nop Foo class self? Don’t you mean this? @interface Foo : NSObject - (id)initWithBar:(NSString *)bar; @end @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { self = [super init]; if (self != nil) { _bar = [bar copy]; } return self; } @end .h .m

Slide 47

Slide 47 text

3 nil? Don’t you mean null? Nop Foo class self? Don’t you mean this? Nop @interface Foo : NSObject - (id)initWithBar:(NSString *)bar; @end @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { self = [super init]; if (self != nil) { _bar = [bar copy]; } return self; } @end .h .m

Slide 48

Slide 48 text

3Foo class @interface Foo : NSObject - (id)initWithBar:(NSString *)bar; @end @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { self = [super init]; if (self != nil) { _bar = [bar copy]; } return self; } @end .h .m

Slide 49

Slide 49 text

3+ & - @interface Foo : NSObject - (int)sumA:(int)a withB:(int)b; + (Foo *)aFoo; @end @implementation Foo - (int)sumA:(int)a withB:(int)b { return a+b; } + (Foo *)aFoo { return [[Foo alloc] init]; } @end .h .m

Slide 50

Slide 50 text

3Properties @interface Foo : NSObject @property (nonatomic, copy) NSString *bar; @end .h @implementation Foo @synthesize bar; @end .m class Foo { private String bar; public String getBar() { return bar; } public void setBar(String bar) { this.bar = bar; } }

Slide 51

Slide 51 text

3NSWhat? "I'm pretty sure that Java's 'interface' is a direct rip-off of Obj-C's 'protocol' which was largely designed by these ex-NeXT'ers..." Patrick Naughton (one of the original creators of Java)

Slide 52

Slide 52 text

3Protocols interface Color { public int getRed(); public int getBlue(); public int getGreen(); } @protocol Color - (int)red; - (int)green; - (int)blue; @opcional - (int)rgb; @end

Slide 53

Slide 53 text

3Categories @interface NSString (MD5) - (NSString *)md5Hash; @end @implementation NSString (MD5) - (NSString *)md5Hash { NSString *md5 = anMD5Func(self); return md5; } @end .h .m

Slide 54

Slide 54 text

Recap PROPERTIES ARE AWESOME IMPLEMENTATION .M Interface .H Interfaces are called protocols and there’s no abstract classes

Slide 55

Slide 55 text

Recap PROPERTIES ARE AWESOME IMPLEMENTATION .M Interface .H Interfaces are called protocols and there’s no abstract classes And imagine what you can do with categories

Slide 56

Slide 56 text

4OBjective-c For Java developers remember c?

Slide 57

Slide 57 text

4NOOO! @implementation Foo - (int)anAddress { char *bar = malloc(1); free(bar); return &bar; } @end int main(int argc, char *argv[]) { @autoreleasepool { Foo *foo = [[Foo alloc] init]; printf("0x%X",[foo anAddress]); } } Foo.m main.m

Slide 58

Slide 58 text

4Why C? "Objective-C is a hybrid programming language[…]formed by grafting the Smalltalk-80 style of object-oriented programming onto a C language rootstock. Objective-C adds precisely one new data type, the object[...]and precisely one new operation, the message expression. " Cox, Brad J. (1991). Object Oriented Programming: An Evolutionary Approach

Slide 59

Slide 59 text

4The Object typedef struct objc_object { Class isa; } *id; typedef struct objc_class *Class; objc.h struct objc_class { Class isa; }; runtime.h

Slide 60

Slide 60 text

4CF Core Foundation is a library with a set of programming interfaces conceptually derived from the Objective-C-based Foundation framework but implemented in the C language. To do this, Core Foundation implements a limited object model in C. Core Foundation defines opaque types that encapsulate data and functions, hereafter referred to as “objects.”

Slide 61

Slide 61 text

Recap C Is at the bottom of objective-c and YOU SHOULD TAKE ADVANTAGE OF IT

Slide 62

Slide 62 text

Recap C Is at the bottom of objective-c and YOU SHOULD TAKE ADVANTAGE OF IT BUT C DOES NOT HAVE ARC SO YOU ARE STUCK WITH MALLOC AND FREE

Slide 63

Slide 63 text

5OBjective-c For Java developers BLOCKS

Slide 64

Slide 64 text

5Closures //create the array NSArray *arr = @[ @16, @11, @88 ]; //sort it using a block NSArray *sortedArr = [arr sortedArrayUsingComparator:^(id obj1, id obj2) { return [((NSNumber *)obj1) compare:(NSNumber *)obj2]; }]; //print NSLog(@"%@",sortedArr);

Slide 65

Slide 65 text

5Keep it //save a block in a variable int (^aBlock)(int, int) = ^(int i, int j) { return i * j; }; //execute it int result = aBlock(1,2);

Slide 66

Slide 66 text

5Get it //get the block as a parameter - (int)operationWithArg1:(int)i arg2:(int)j block:(int (^)(int, int))aBlock { return aBlock(i,j); }

Slide 67

Slide 67 text

5Blocks & Memory “If you are using ARC, object variables are retained and released automatically as the block is copied and later released.” “If you use a block within the implementation of a method [...] If you access an instance variable by reference, self is retained; If you access an instance variable by value, the variable is retained.” “When you copy a stack-based block, you get a new block. If you copy a heap-based block, however, you simply increment the retain count of that block and get it back as the returned value of the copy function or method.”

Slide 68

Slide 68 text

Recap BLOCKS Is MY FAVORITE OBJECTIVE-C LIBRARY AND IT WILL BE ONE OF YOURS TOO

Slide 69

Slide 69 text

Recap BLOCKS Is MY FAVORITE OBJECTIVE-C LIBRARY AND IT WILL BE ONE OF YOURS TOO The notation is hard to MASTER though

Slide 70

Slide 70 text

6OBjective-c For Java developers Other stuff

Slide 71

Slide 71 text

6Ϋʔϧʂ NSString *localizedAbout = NSLocalizedString(@"About", nil); ... "About" = "Sobre"; "Open" = "Abrir"; "Close" = "Fechar"; ... Localizable.strings

Slide 72

Slide 72 text

6Read it - (void)insertSublayer:(CALayer *)layer below:(CALayer *)sibling; - (void)insertSublayer:(CALayer *)layer above:(CALayer *)sibling; - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx; - (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately: (BOOL)startImmediately NS_AVAILABLE(10_5, 2_0);

Slide 73

Slide 73 text

Recap this is just the beginning. There’s a lot more to objective-c

Slide 74

Slide 74 text

Recap this is just the beginning. There’s a lot more to objective-c But now it won’t scare you !

Slide 75

Slide 75 text

7OBjective-c For Java developers DEMO

Slide 76

Slide 76 text

f r @ fr f r w I’ @fbb r r w r fb r r b