76
Objective-c FOR JAVA DEVELOPERS

Objective-C for Java developers

Embed Size (px)

DESCRIPTION

This talk was created to ease the transition between Java and Objective-C. The demo code is at: https://github.com/fbernardo/SpellingCorrector

Citation preview

Page 1: Objective-C for Java developers

Objective-cFOR

JAVA DEVELOPERS

Page 2: Objective-C for Java developers

1OBjective-c For Java developers

The basics

Page 3: Objective-C for Java developers

1Hello world

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

Page 4: Objective-C for Java developers

1Basic types

byte aByte = 0;short aShort = 0;int anInt = 0;long aLong = 0; float aFloat = 0;double aDouble = 0;boolean aBoolean = true; //falsechar 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; //NOchar aChar = 'a';NSObject *anObject = [[NSObject alloc] init];

Page 5: Objective-C for Java developers

1Methods

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

Page 6: Objective-C for Java developers

1Methods

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

Page 7: Objective-C for Java developers

1Methods

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

Page 8: Objective-C for Java developers

1Methods

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

Page 9: Objective-C for Java developers

1Methods

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

Page 10: Objective-C for Java developers

thebasics

arethe

same!

Recap

Page 11: Objective-C for Java developers

thebasics

arethe

same!also, YAY

for unsigned

Recap

Page 12: Objective-C for Java developers

2OBjective-c For Java developers

Meet the objects

Page 13: Objective-C for Java developers

2Pointers

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

Page 14: Objective-C for Java developers

2Pointers

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

Stuff0x3DE2FE

Page 15: Objective-C for Java developers

2Pointers

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

Stuff0x3DE2FE

Page 16: Objective-C for Java developers

2Pointers

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

Stuff0x3DE2FE

Page 17: Objective-C for Java developers

2Pointers

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

Stuff0x3DE2FE Object*

Page 18: Objective-C for Java developers

2Pointers

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

Stuff0x3DE2FE

Page 19: Objective-C for Java developers

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)

Page 20: Objective-C for Java developers

2String

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

initWithString:@"A string."];

Page 21: Objective-C for Java developers

2String

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

initWithString:@"A string."];

Page 22: Objective-C for Java developers

2String

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

initWithString:@"A string."];

String aString = "A string."; NSString *aString = @"A string.";

Page 23: Objective-C for Java developers

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

Page 24: Objective-C for Java developers

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

Page 25: Objective-C for Java developers

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

Page 26: Objective-C for Java developers

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

Page 27: Objective-C for Java developers

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

Page 28: Objective-C for Java developers

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

Page 29: Objective-C for Java developers

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?

Page 30: Objective-C for Java developers

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 :(

Page 31: Objective-C for Java developers

2(NS)Array

String strings[] = {"1", "2"}; NSArray *strings = @[@"1", @"2"];

Page 32: Objective-C for Java developers

2(NS)Array

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

Page 33: Objective-C for Java developers

List<String> strings = new ArrayList<String>(10);

NSMutableArray *strings = [NSMutableArray arrayWithCapacity:10];

2(NS)Array

Page 34: Objective-C for Java developers

2Dictionary

Map<Object, Object> m = new HashMap<Object,Object>();m.put("key","value");

NSDictionary *m = @{@"key" : @"value"};

Page 35: Objective-C for Java developers

2Dictionary

Map<Object, Object> m = new HashMap<Object,Object>();m.put("key","value");

NSMutableDictionary *m = [@{@"key" : @"value"} mutableCopy];[m setObject:@"key2" forKey:@"value2"];

Page 36: Objective-C for Java developers

2Dictionary

Map<Object, Object> m = new HashMap<Object,Object>();m.put("key","value");

NSMutableDictionary *m = [@{@"key" : @"value"} mutableCopy];[m setObject:@"key2" forKey:@"value2"];

Page 37: Objective-C for Java developers

2Dictionary

Map<Object, Object> m = new HashMap<Object,Object>();m.put("key","value");

NSMutableDictionary *m = [@{@"key" : @"value"} mutableCopy];[m setObject:@"key2" forKey:@"value2"];

Page 38: Objective-C for Java developers

2Literals

String s = "value1";Number n = 3.14159265359;String a[] = {"value1","value2"};Map<String,String> m = new HashMap<String,String>();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";

Page 39: Objective-C for Java developers

RecapPOINTER =OBJECT

LITERALS ARE AWESOME

INIT INITIALIZEALLOC CREATE

Page 40: Objective-C for Java developers

RecapPOINTER =OBJECT

STRINGCONCATSUCKS

LITERALS ARE AWESOME

INIT INITIALIZEALLOC CREATE

Page 41: Objective-C for Java developers

3OBjective-c For Java developers

Interfaces & implementations

Page 42: Objective-C for Java developers

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

Page 43: Objective-C for Java developers

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

Page 44: Objective-C for Java developers

3nil? 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

Page 45: Objective-C for Java developers

3nil? 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

Page 46: Objective-C for Java developers

3nil? Don’t you mean null?

Nop

Foo classself? 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

Page 47: Objective-C for Java developers

3nil? Don’t you mean null?

Nop

Foo classself? 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

Page 48: Objective-C for Java developers

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

Page 49: Objective-C for Java developers

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

Page 50: Objective-C for Java developers

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

Page 51: Objective-C for Java developers

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)

Page 52: Objective-C for Java developers

3Protocols

interface Color { public int getRed(); public int getBlue(); public int getGreen();}

@protocol Color <NSObject>

- (int)red;- (int)green;- (int)blue;

@opcional- (int)rgb;

@end

Page 53: Objective-C for Java developers

3Categories

@interface NSString (MD5)

- (NSString *)md5Hash;

@end

@implementation NSString (MD5)

- (NSString *)md5Hash { NSString *md5 = anMD5Func(self); return md5;}

@end

.h .m

Page 54: Objective-C for Java developers

Recap

PROPERTIES ARE

AWESOME

IMPLEMENTATION .MInterface .H

Interfaces are called protocols and there’s

no abstract classes

Page 55: Objective-C for Java developers

Recap

PROPERTIES ARE

AWESOME

IMPLEMENTATION .MInterface .H

Interfaces are called protocols and there’s

no abstract classesAnd

imagine what you

can do with

categories

Page 56: Objective-C for Java developers

4OBjective-c For Java developers

remember c?

Page 57: Objective-C for Java developers

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

Page 58: Objective-C for Java developers

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

Page 59: Objective-C for Java developers

4The Object

typedef struct objc_object { Class isa;} *id;

typedef struct objc_class *Class;

objc.h

struct objc_class { Class isa;};

runtime.h

Page 60: Objective-C for Java developers

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.”

Page 61: Objective-C for Java developers

RecapC Is at the bottom of

objective-c and YOU

SHOULD TAKE ADVANTAGE OF

IT

Page 62: Objective-C for Java developers

RecapC 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

Page 63: Objective-C for Java developers

5OBjective-c For Java developers

BLOCKS

Page 64: Objective-C for Java developers

5Closures

//create the arrayNSArray *arr = @[ @16, @11, @88 ];

//sort it using a blockNSArray *sortedArr = [arr sortedArrayUsingComparator:^(id obj1, id obj2) { return [((NSNumber *)obj1) compare:(NSNumber *)obj2];}];

//printNSLog(@"%@",sortedArr);

Page 65: Objective-C for Java developers

5Keep it

//save a block in a variableint (^aBlock)(int, int) = ^(int i, int j) { return i * j; };

//execute itint result = aBlock(1,2);

Page 66: Objective-C for Java developers

5Get it

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

Page 67: Objective-C for Java developers

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.”

Page 68: Objective-C for Java developers

RecapBLOCKS Is MY

FAVORITE OBJECTIVE-C

LIBRARY AND IT WILL BE ONE OF

YOURS TOO

Page 69: Objective-C for Java developers

RecapBLOCKS Is MY

FAVORITE OBJECTIVE-C

LIBRARY AND IT WILL BE ONE OF

YOURS TOO

The notation is hard to

MASTER though

Page 70: Objective-C for Java developers

6OBjective-c For Java developers

Other stuff

Page 71: Objective-C for Java developers

6クール!NSString *localizedAbout = NSLocalizedString(@"About", nil);

..."About" = "Sobre";"Open" = "Abrir";"Close" = "Fechar";...

Localizable.strings

Page 72: Objective-C for Java developers

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

Page 73: Objective-C for Java developers

Recap

this is just the beginning.

There’s a lot more to

objective-c

Page 74: Objective-C for Java developers

Recap

this is just the beginning.

There’s a lot more to

objective-c But now it won’t

scare you !

Page 75: Objective-C for Java developers

7OBjective-c For Java developers

DEMO

Page 76: Objective-C for Java developers

!"#$% f&r

'(%)*#(#+!"#$% )& @,-+&.%fr"#/" f&r ),* "w*%&0* 1*%(+# %-++*%)(&#%

I’0 @fbb*r#"r1& &# )w())*r "#1 fb*r#"r1& &# +(),-b