Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Objective-C for Java developers

Objective-C for Java developers

A simple presentation for who's coming from a Java background and trying to get into Objective-C .

Fábio Bernardo

December 04, 2012
Tweet

More Decks by Fábio Bernardo

Other Decks in Programming

Transcript

  1. 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];
  2. 2Pointers String s = new String(); NSString *s = [[NSString

    alloc] init]; Stuff 0x3DE2FE Object *
  3. 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)
  4. 2String String aString = new String("A string."); NSString *aString =

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

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

    [[NSString alloc] initWithString:@"A string."]; String aString = "A string."; NSString *aString = @"A string.";
  7. 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];
  8. 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];
  9. 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];
  10. 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];
  11. 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];
  12. 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];
  13. 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?
  14. 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 :(
  15. 2Dictionary Map<Object, Object> m = new HashMap<Object,Object>(); m.put("key","value"); NSMutableDictionary *m

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

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

    = [@{@"key" : @"value"} mutableCopy]; [m setObject:@"key2" forKey:@"value2"];
  18. 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";
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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; } }
  28. 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)
  29. 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
  30. 3Categories @interface NSString (MD5) - (NSString *)md5Hash; @end @implementation NSString

    (MD5) - (NSString *)md5Hash { NSString *md5 = anMD5Func(self); return md5; } @end .h .m
  31. Recap PROPERTIES ARE AWESOME IMPLEMENTATION .M Interface .H Interfaces are

    called protocols and there’s no abstract classes
  32. 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
  33. 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
  34. 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
  35. 4The Object typedef struct objc_object { Class isa; } *id;

    typedef struct objc_class *Class; objc.h struct objc_class { Class isa; }; runtime.h
  36. 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.”
  37. 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
  38. 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);
  39. 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);
  40. 5Get it //get the block as a parameter - (int)operationWithArg1:(int)i

    arg2:(int)j block:(int (^)(int, int))aBlock { return aBlock(i,j); }
  41. 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.”
  42. Recap BLOCKS Is MY FAVORITE OBJECTIVE-C LIBRARY AND IT WILL

    BE ONE OF YOURS TOO The notation is hard to MASTER though
  43. 6Ϋʔϧʂ NSString *localizedAbout = NSLocalizedString(@"About", nil); ... "About" = "Sobre";

    "Open" = "Abrir"; "Close" = "Fechar"; ... Localizable.strings
  44. 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);
  45. Recap this is just the beginning. There’s a lot more

    to objective-c But now it won’t scare you !