Your SlideShare is downloading. ×
0
Objective-cFORJAVADEVELOPERS
1The basics OBjective-c For Java developers
System.out.println("Hello, World!");   NSLog(@"Hello, World!");1Hello world
byte aByte = 0;                    char aByte = 0;short aShort = 0;                  short aShort = 0;int anInt = 0;      ...
NSString *aString = [[NSString alloc]String aString = new String(chars);                                                  ...
NSString *aString = [[NSString alloc]String aString = new String(chars);                                                  ...
NSString *aString = [[NSString alloc]String aString = new String(chars);                                                  ...
NSString *aString = [[NSString alloc]String aString = new String(chars);                                                  ...
NSString *aString = [[NSString alloc]String aString = new String(chars);                                                  ...
Recap thebasics are thesame!
Recap thebasics are the     also, YAYsame!       for         unsigned
2Meet the objects OBjective-c For Java developers
String s = new String();   NSString *s = [[NSString alloc] init];2Pointers
String s = new String();   NSString *s = [[NSString alloc] init]; 0x3DE2FE                                 Stuff2Pointers
String s = new String();   NSString *s = [[NSString alloc] init]; 0x3DE2FE                                 Stuff2Pointers
String s = new String();   NSString *s = [[NSString alloc] init]; 0x3DE2FE                                 Stuff2Pointers
String s = new String();   NSString *s = [[NSString alloc] init]; 0x3DE2FE    *                                  Object   ...
String s = new String();   NSString *s = [[NSString alloc] init]; 0x3DE2FE                                 Stuff2Pointers
"Many of those strange primitive wrapper classes, like Integer andNumber came from Lee Boynton, one of the early NeXT Obj-...
NSString *aString = [[NSString alloc]String aString = new String("A string.");                                            ...
NSString *aString = [[NSString alloc]String aString = new String("A string.");                                            ...
NSString *aString = [[NSString alloc]String aString = new String("A string.");                                            ...
NSString *one = @"1 +";String one = "1 + ";                                         int two = 2;int two = 2;              ...
NSString *one = @"1 +";String one = "1 + ";                                         int two = 2;int two = 2;              ...
NSString *one = @"1 +";String one = "1 + ";                                         int two = 2;int two = 2;              ...
NSString *one = @"1 +";String one = "1 + ";                                         int two = 2;int two = 2;              ...
NSString *one = @"1 +";String one = "1 + ";                                         int two = 2;int two = 2;              ...
NSString *one = @"1 +";String one = "1 + ";                                         int two = 2;int two = 2;              ...
But... If I can useNSStringthen I +";                                                  C++ *one = @"1String one = "1 + ";i...
But... If I can useNSStringthen I +";                                                  C++ *one = @"1String one = "1 + ";i...
String strings[] = {"1", "2"};   NSArray *strings = @[@"1", @"2"];2(NS)Array
NSString **strings =String strings[] = new String[10];                                                malloc(10*sizeof(NSS...
List<String> strings =                    NSMutableArray *strings =             new ArrayList<String>(10);          [NSMut...
Map<Object, Object> m = new                    HashMap<Object,Object>();   NSDictionary *m = @{@"key" : @"value"};m.put("k...
Map<Object, Object> m = new                     NSMutableDictionary *m =                    HashMap<Object,Object>();     ...
Map<Object, Object> m = new                     NSMutableDictionary *m =                    HashMap<Object,Object>();     ...
Map<Object, Object> m = new                     NSMutableDictionary *m =                    HashMap<Object,Object>();     ...
NSString *s = @"value1";String s = "value1";                             NSNumber *n = @3.14159265359;Number n = 3.1415926...
POINTER                 Recap    = OBJECTALLOC       CREATE INIT    INITIALIZE   LITERALS ARE     AWESOME
POINTER                  Recap    = OBJECTALLOC       CREATE INIT    INITIALIZE   LITERALS ARE   STRING                  C...
OBjective-c For Java developers3implementations Interfaces &
.h                                                             .m                                    @implementation Foo {...
.h                                                                          .m                                      @imple...
.h                                                                                       .m                               ...
.h                                                                                       .m                               ...
.h                                                                                  .m                                    ...
.h                                                                                  .m                                    ...
.h                                                                          .m                                      @imple...
.h                                                                      .m                                    @implementat...
.hclass Foo {                                       @interface Foo : NSObject    private String bar;                      ...
"Im pretty sure that Javas interface is a direct rip-off of Obj-Cs protocolwhich was largely designed by these ex-NeXTers.....
@protocol Color <NSObject>                             - (int)red;interface Color {                             - (int)gre...
.h                                                                .m                             @implementation NSString ...
Recap    Interface               .HIMPLEMENTATION              .M    Interfaces are called    protocols and there’s     no...
Recap    Interface            .HIMPLEMENTATION           .M    Interfaces are called    protocols and there’s   And     no...
4remember c? OBjective-c For Java developers
Foo.m                                                              main.m @implementation Foo                            i...
"Objective-C is a hybrid programming language[…]formed by grafting theSmalltalk-80 style of object-oriented programming on...
objc.h                                                     runtime.h typedef struct objc_object {     Class isa;          ...
Core Foundation is a library with a set of programming interfacesconceptually derived from the Objective-C-based Foundatio...
Recap  C Is at the  bottom of objective-c   and YOU SHOULD TAKEADVANTAGE OF       IT
Recap  C Is at the  bottom of objective-c BUT C   and YOU    DOES NOT              HAVE ARC SHOULD TAKE SO YOU            ...
5BLOCKS OBjective-c For Java developers
//create the arrayNSArray *arr = @[ @16, @11, @88 ];//sort it using a blockNSArray *sortedArr = [arr sortedArrayUsingCompa...
//save a block in a variableint (^aBlock)(int, int) = ^(int i, int j) { return i * j; };//execute itint result = aBlock(1,...
//get the block as a parameter- (int)operationWithArg1:(int)i arg2:(int)j block:(int (^)(int, int))aBlock {    return aBlo...
“If you are using ARC, object variables are retained and released automatically as the block is copied and laterreleased.”...
Recap BLOCKS Is MY   FAVORITE OBJECTIVE-CLIBRARY AND ITWILL BE ONE OF  YOURS TOO
Recap BLOCKS Is MY   FAVORITE OBJECTIVE-CLIBRARY AND IT TheWILL BE ONE OF notation              is hard to  YOURS TOO     ...
6Other stuff OBjective-c For Java developers
Localizable.strings                                               ...                                               "About...
- (void)insertSublayer:(CALayer *)layer below:(CALayer *)sibling;- (void)insertSublayer:(CALayer *)layer above:(CALayer *)...
Recapthis is just the  beginning. There’s a lot    more to objective-c
Recapthis is just the  beginning. There’s a lot    more to                 But now objective-c it won’t                  s...
7DEMO OBjective-c For Java developers
Thanks                       for                   listeningI’m @fbbernardo on twitter and fbernardo on github      Thanks...
Upcoming SlideShare
Loading in...5
×

Objective-C for Java developers

1,308

Published on

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

Published in: Technology
2 Comments
3 Likes
Statistics
Notes
  • @vcharles80 Thank you. Unfortunately it wasn't recorded, I only have the slides. But I can try to point you in the right direction if you're having problems with learning Objective-C.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • This is awesome stuff... Would be really grateful if you could upload a audio/video of this presentation...
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
1,308
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
54
Comments
2
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Objective-C for Java developers"

  1. 1. Objective-cFORJAVADEVELOPERS
  2. 2. 1The basics OBjective-c For Java developers
  3. 3. System.out.println("Hello, World!"); NSLog(@"Hello, World!");1Hello world
  4. 4. byte aByte = 0; char aByte = 0;short aShort = 0; short aShort = 0;int anInt = 0; int anInt = 0;long aLong = 0; long aLong = 0;float aFloat = 0; float aFloat;double aDouble = 0; double aDouble;boolean aBoolean = true; //false BOOL aBoolean = YES; //NOchar aChar = a; char aChar = a;1Basic typesObject anObject = new Object(); NSObject *anObject = [[NSObject alloc] init];
  5. 5. NSString *aString = [[NSString alloc]String aString = new String(chars); initWithUTF8String:chars];1Methods
  6. 6. NSString *aString = [[NSString alloc]String aString = new String(chars); initWithUTF8String:chars];1Methods
  7. 7. NSString *aString = [[NSString alloc]String aString = new String(chars); initWithUTF8String:chars];1Methods
  8. 8. NSString *aString = [[NSString alloc]String aString = new String(chars); initWithUTF8String:chars];1Methods
  9. 9. NSString *aString = [[NSString alloc]String aString = new String(chars); initWithUTF8String:chars];1Methods
  10. 10. Recap thebasics are thesame!
  11. 11. Recap thebasics are the also, YAYsame! for unsigned
  12. 12. 2Meet the objects OBjective-c For Java developers
  13. 13. String s = new String(); NSString *s = [[NSString alloc] init];2Pointers
  14. 14. String s = new String(); NSString *s = [[NSString alloc] init]; 0x3DE2FE Stuff2Pointers
  15. 15. String s = new String(); NSString *s = [[NSString alloc] init]; 0x3DE2FE Stuff2Pointers
  16. 16. String s = new String(); NSString *s = [[NSString alloc] init]; 0x3DE2FE Stuff2Pointers
  17. 17. String s = new String(); NSString *s = [[NSString alloc] init]; 0x3DE2FE * Object Stuff2Pointers
  18. 18. String s = new String(); NSString *s = [[NSString alloc] init]; 0x3DE2FE Stuff2Pointers
  19. 19. "Many of those strange primitive wrapper classes, like Integer andNumber came from Lee Boynton, one of the early NeXT Obj-C class libraryguys who hated int and float types."Patrick Naughton (one of the original creators of Java)2NSWhat?
  20. 20. NSString *aString = [[NSString alloc]String aString = new String("A string."); initWithString:@"A string."];2String
  21. 21. NSString *aString = [[NSString alloc]String aString = new String("A string."); initWithString:@"A string."];2String
  22. 22. NSString *aString = [[NSString alloc]String aString = new String("A string."); initWithString:@"A string."]; String aString = "A string."; NSString *aString = @"A string.";2String
  23. 23. NSString *one = @"1 +";String one = "1 + "; int two = 2;int two = 2; NSString *eq = @"=";String equals = " = "; float three = 3.0;float three = 3.0f; NSString *s = [NSString stringWithFormat:@"%@String s = one + two + equals + three; %d %@ %.1f", one, two, eq, three];2“A” + “B”
  24. 24. NSString *one = @"1 +";String one = "1 + "; int two = 2;int two = 2; NSString *eq = @"=";String equals = " = "; float three = 3.0;float three = 3.0f; NSString *s = [NSString stringWithFormat:@"%@String s = one + two + equals + three; %d %@ %.1f", one, two, eq, three];2“A” + “B”
  25. 25. NSString *one = @"1 +";String one = "1 + "; int two = 2;int two = 2; NSString *eq = @"=";String equals = " = "; float three = 3.0;float three = 3.0f; NSString *s = [NSString stringWithFormat:@"%@String s = one + two + equals + three; %d %@ %.1f", one, two, eq, three];2“A” + “B”
  26. 26. NSString *one = @"1 +";String one = "1 + "; int two = 2;int two = 2; NSString *eq = @"=";String equals = " = "; float three = 3.0;float three = 3.0f; NSString *s = [NSString stringWithFormat:@"%@String s = one + two + equals + three; %d %@ %.1f", one, two, eq, three];2“A” + “B”
  27. 27. NSString *one = @"1 +";String one = "1 + "; int two = 2;int two = 2; NSString *eq = @"=";String equals = " = "; float three = 3.0;float three = 3.0f; NSString *s = [NSString stringWithFormat:@"%@String s = one + two + equals + three; %d %@ %.1f", one, two, eq, three];2“A” + “B”
  28. 28. NSString *one = @"1 +";String one = "1 + "; int two = 2;int two = 2; NSString *eq = @"=";String equals = " = "; float three = 3.0;float three = 3.0f; NSString *s = [NSString stringWithFormat:@"%@String s = one + two + equals + three; %d %@ %.1f", one, two, eq, three];2“A” + “B”
  29. 29. But... If I can useNSStringthen I +"; C++ *one = @"1String one = "1 + ";int two = 2; can use operator overloading int two = 2; NSString *eq = @"=";String equals = " = ";float three = 3.0f; to add that to Objective-C, float three = 3.0; NSString *s = [NSString stringWithFormat:@"%@String s = one + two + equals + three; right?%d %@ %.1f", one, two, eq, three];2“A” + “B”
  30. 30. But... If I can useNSStringthen I +"; C++ *one = @"1String one = "1 + ";int two = 2; can use operator overloading int two = 2; NSString *eq = @"=";String equals = " = ";float three = 3.0f; to add that to Objective-C, float three = 3.0; NSString *s = [NSString stringWithFormat:@"%@String s = one + two + equals + three; right?%d %@ %.1f", one, two, eq, three]; Nop :(2“A” + “B”
  31. 31. String strings[] = {"1", "2"}; NSArray *strings = @[@"1", @"2"];2(NS)Array
  32. 32. NSString **strings =String strings[] = new String[10]; malloc(10*sizeof(NSString *));2(NS)Array
  33. 33. List<String> strings = NSMutableArray *strings = new ArrayList<String>(10); [NSMutableArray arrayWithCapacity:10];2(NS)Array
  34. 34. Map<Object, Object> m = new HashMap<Object,Object>(); NSDictionary *m = @{@"key" : @"value"};m.put("key","value");2Dictionary
  35. 35. Map<Object, Object> m = new NSMutableDictionary *m = HashMap<Object,Object>(); [@{@"key" : @"value"} mutableCopy];m.put("key","value"); [m setObject:@"key2" forKey:@"value2"];2Dictionary
  36. 36. Map<Object, Object> m = new NSMutableDictionary *m = HashMap<Object,Object>(); [@{@"key" : @"value"} mutableCopy];m.put("key","value"); [m setObject:@"key2" forKey:@"value2"];2Dictionary
  37. 37. Map<Object, Object> m = new NSMutableDictionary *m = HashMap<Object,Object>(); [@{@"key" : @"value"} mutableCopy];m.put("key","value"); [m setObject:@"key2" forKey:@"value2"];2Dictionary
  38. 38. NSString *s = @"value1";String s = "value1"; NSNumber *n = @3.14159265359;Number n = 3.14159265359; NSArray *a = @[@"value1", @"value2"];String a[] = {"value1","value2"}; NSDictionary *m = @{@"key" : @"value"};Map<String,String> m = new NSArray *arr = @[ @(roundf(2*M_PI)) ]; HashMap<String,String>();m.put("key","value"); NSMutableDictionary *dict =Number arr[] = { Math.round(2*Math.PI) }; [NSMutableDictionary dictionary];2Literals dict[@"key1"] = @"value1";
  39. 39. POINTER Recap = OBJECTALLOC CREATE INIT INITIALIZE LITERALS ARE AWESOME
  40. 40. POINTER Recap = OBJECTALLOC CREATE INIT INITIALIZE LITERALS ARE STRING CONCAT AWESOME SUCKS
  41. 41. OBjective-c For Java developers3implementations Interfaces &
  42. 42. .h .m @implementation Foo { @interface Foo : NSObject { //Private ivars //protected/private/public ivars } } //Synthesize properties //Property declarations //Method implementations //Method declarations //Private methods 3.h & .m @end @end
  43. 43. .h .m @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { @interface Foo : NSObject self = [super init]; - (id)initWithBar:(NSString *)bar; if (self != nil) { @end _bar = [bar copy]; } return self; 3Foo class } @end
  44. 44. .h .m @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { @interface Foo : NSObject nil? Don’t you mean null? init]; self = [super - (id)initWithBar:(NSString *)bar; if (self != nil) { @end _bar = [bar copy]; } return self; 3Foo class } @end
  45. 45. .h .m @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { @interface Foo : NSObject nil? Don’t you mean null? init]; self = [super - (id)initWithBar:(NSString *)bar; if (self != nil) { @end Nop } _bar = [bar copy]; return self; 3Foo class } @end
  46. 46. .h .m @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { @interface Foo : NSObject nil? Don’t you mean null? init]; self = [super - (id)initWithBar:(NSString *)bar; if (self != nil) { @end Nop } _bar = [bar copy]; return self; self? Don’t you mean this? 3Foo class } @end
  47. 47. .h .m @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { @interface Foo : NSObject nil? Don’t you mean null? init]; self = [super - (id)initWithBar:(NSString *)bar; if (self != nil) { @end Nop } _bar = [bar copy]; return self; self? Don’t you mean this? 3Foo class } Nop @end
  48. 48. .h .m @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { @interface Foo : NSObject self = [super init]; - (id)initWithBar:(NSString *)bar; if (self != nil) { @end _bar = [bar copy]; } return self; 3Foo class } @end
  49. 49. .h .m @implementation Foo @interface Foo : NSObject - (int)sumA:(int)a withB:(int)b { return a+b; - (int)sumA:(int)a withB:(int)b; } + (Foo *)aFoo; + (Foo *)aFoo { @end return [[Foo alloc] init]; } 3+ & - @end
  50. 50. .hclass Foo { @interface Foo : NSObject private String bar; @property (nonatomic, copy) NSString *bar; @end public String getBar() { return bar; } .m public void setBar(String bar) { @implementation Foo this.bar = bar; @synthesize bar; }3Properties @end}
  51. 51. "Im pretty sure that Javas interface is a direct rip-off of Obj-Cs protocolwhich was largely designed by these ex-NeXTers..."Patrick Naughton (one of the original creators of Java)3NSWhat?
  52. 52. @protocol Color <NSObject> - (int)red;interface Color { - (int)green; public int getRed(); - (int)blue; public int getBlue(); public int getGreen(); @opcional} - (int)rgb;3Protocols @end
  53. 53. .h .m @implementation NSString (MD5) @interface NSString (MD5) - (NSString *)md5Hash { NSString *md5 = anMD5Func(self); - (NSString *)md5Hash; return md5; } @end 3Categories @end
  54. 54. Recap Interface .HIMPLEMENTATION .M Interfaces are called protocols and there’s no abstract classes PROPERTIES ARE AWESOME
  55. 55. Recap Interface .HIMPLEMENTATION .M Interfaces are called protocols and there’s And no abstract classes imagine PROPERTIES what you can do ARE with AWESOME categories
  56. 56. 4remember c? OBjective-c For Java developers
  57. 57. Foo.m main.m @implementation Foo int main(int argc, char *argv[]) { - (int)anAddress { @autoreleasepool { char *bar = malloc(1); Foo *foo = [[Foo alloc] init]; free(bar); printf("0x%X",[foo anAddress]); return &bar; } } } 4NOOO! @end
  58. 58. "Objective-C is a hybrid programming language[…]formed by grafting theSmalltalk-80 style of object-oriented programming onto a C languagerootstock. Objective-C adds precisely one new data type, theobject[...]and precisely one new operation, the message expression. "4Why C?Cox, Brad J. (1991). Object Oriented Programming: An Evolutionary Approach
  59. 59. objc.h runtime.h typedef struct objc_object { Class isa; struct objc_class { } *id; Class isa; }; typedef struct objc_class *Class; 4The Object
  60. 60. Core Foundation is a library with a set of programming interfacesconceptually derived from the Objective-C-based Foundation frameworkbut implemented in the C language. To do this, Core Foundationimplements a limited object model in C. Core Foundation defines opaquetypes that encapsulate data and functions, hereafter referred to as4CF“objects.”
  61. 61. Recap C Is at the bottom of objective-c and YOU SHOULD TAKEADVANTAGE OF IT
  62. 62. Recap C Is at the bottom of objective-c BUT C and YOU DOES NOT HAVE ARC SHOULD TAKE SO YOU ARE STUCKADVANTAGE OF WITH IT MALLOC AND FREE
  63. 63. 5BLOCKS OBjective-c For Java developers
  64. 64. //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];}];//print5ClosuresNSLog(@"%@",sortedArr);
  65. 65. //save a block in a variableint (^aBlock)(int, int) = ^(int i, int j) { return i * j; };//execute itint result = aBlock(1,2);5Keep it
  66. 66. //get the block as a parameter- (int)operationWithArg1:(int)i arg2:(int)j block:(int (^)(int, int))aBlock { return aBlock(i,j);}5Get it
  67. 67. “If you are using ARC, object variables are retained and released automatically as the block is copied and laterreleased.”“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, yousimply increment the retain count of that block and get it back as the returned value of the copy function or5Memorymethod.” Blocks &
  68. 68. Recap BLOCKS Is MY FAVORITE OBJECTIVE-CLIBRARY AND ITWILL BE ONE OF YOURS TOO
  69. 69. Recap BLOCKS Is MY FAVORITE OBJECTIVE-CLIBRARY AND IT TheWILL BE ONE OF notation is hard to YOURS TOO MASTER though
  70. 70. 6Other stuff OBjective-c For Java developers
  71. 71. Localizable.strings ... "About" = "Sobre";NSString *localizedAbout = "Open" = "Abrir"; NSLocalizedString(@"About", nil); "Close" = "Fechar"; ...6クール!
  72. 72. - (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);6Read it
  73. 73. Recapthis is just the beginning. There’s a lot more to objective-c
  74. 74. Recapthis is just the beginning. There’s a lot more to But now objective-c it won’t scare you !
  75. 75. 7DEMO OBjective-c For Java developers
  76. 76. Thanks for listeningI’m @fbbernardo on twitter and fbernardo on github Thanks to @hugojsfranca for the awesome design suggestions
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×