Objective-C for Java developers
Upcoming SlideShare
Loading in...5
×
 

Objective-C for Java developers

on

  • 1,357 views

This talk was created to ease the transition between Java and Objective-C.

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

Statistics

Views

Total Views
1,357
Views on SlideShare
1,338
Embed Views
19

Actions

Likes
3
Downloads
49
Comments
2

3 Embeds 19

http://www.linkedin.com 9
https://twitter.com 7
https://www.linkedin.com 3

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial LicenseCC Attribution-NonCommercial License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • @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.
    Are you sure you want to
    Your message goes here
    Processing…
  • This is awesome stuff... Would be really grateful if you could upload a audio/video of this presentation...
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Objective-C for Java developers Objective-C for Java developers Presentation Transcript

  • Objective-cFORJAVADEVELOPERS
  • 1The basics OBjective-c For Java developers
  • System.out.println("Hello, World!"); NSLog(@"Hello, World!");1Hello world View slide
  • 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]; View slide
  • NSString *aString = [[NSString alloc]String aString = new String(chars); initWithUTF8String:chars];1Methods
  • NSString *aString = [[NSString alloc]String aString = new String(chars); initWithUTF8String:chars];1Methods
  • NSString *aString = [[NSString alloc]String aString = new String(chars); initWithUTF8String:chars];1Methods
  • NSString *aString = [[NSString alloc]String aString = new String(chars); initWithUTF8String:chars];1Methods
  • NSString *aString = [[NSString alloc]String aString = new String(chars); initWithUTF8String:chars];1Methods
  • 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 Stuff2Pointers
  • 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-C class libraryguys who hated int and float types."Patrick Naughton (one of the original creators of Java)2NSWhat?
  • NSString *aString = [[NSString alloc]String aString = new String("A string."); initWithString:@"A string."];2String
  • NSString *aString = [[NSString alloc]String aString = new String("A string."); initWithString:@"A string."];2String
  • NSString *aString = [[NSString alloc]String aString = new String("A string."); initWithString:@"A string."]; String aString = "A string."; NSString *aString = @"A string.";2String
  • 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”
  • 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”
  • 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”
  • 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”
  • 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”
  • 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”
  • 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”
  • 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”
  • String strings[] = {"1", "2"}; NSArray *strings = @[@"1", @"2"];2(NS)Array
  • NSString **strings =String strings[] = new String[10]; malloc(10*sizeof(NSString *));2(NS)Array
  • List<String> strings = NSMutableArray *strings = new ArrayList<String>(10); [NSMutableArray arrayWithCapacity:10];2(NS)Array
  • Map<Object, Object> m = new HashMap<Object,Object>(); NSDictionary *m = @{@"key" : @"value"};m.put("key","value");2Dictionary
  • Map<Object, Object> m = new NSMutableDictionary *m = HashMap<Object,Object>(); [@{@"key" : @"value"} mutableCopy];m.put("key","value"); [m setObject:@"key2" forKey:@"value2"];2Dictionary
  • Map<Object, Object> m = new NSMutableDictionary *m = HashMap<Object,Object>(); [@{@"key" : @"value"} mutableCopy];m.put("key","value"); [m setObject:@"key2" forKey:@"value2"];2Dictionary
  • Map<Object, Object> m = new NSMutableDictionary *m = HashMap<Object,Object>(); [@{@"key" : @"value"} mutableCopy];m.put("key","value"); [m setObject:@"key2" forKey:@"value2"];2Dictionary
  • 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";
  • POINTER Recap = OBJECTALLOC CREATE INIT INITIALIZE LITERALS ARE AWESOME
  • POINTER Recap = OBJECTALLOC CREATE INIT INITIALIZE LITERALS ARE STRING CONCAT AWESOME SUCKS
  • OBjective-c For Java developers3implementations Interfaces &
  • .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
  • .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
  • .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
  • .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
  • .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
  • .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
  • .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
  • .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
  • .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}
  • "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?
  • @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
  • .h .m @implementation NSString (MD5) @interface NSString (MD5) - (NSString *)md5Hash { NSString *md5 = anMD5Func(self); - (NSString *)md5Hash; return md5; } @end 3Categories @end
  • Recap Interface .HIMPLEMENTATION .M Interfaces are called protocols and there’s no abstract classes PROPERTIES ARE AWESOME
  • 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
  • 4remember c? OBjective-c For Java developers
  • 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
  • "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
  • objc.h runtime.h typedef struct objc_object { Class isa; struct objc_class { } *id; Class isa; }; typedef struct objc_class *Class; 4The Object
  • 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.”
  • 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 ARE STUCKADVANTAGE OF WITH IT MALLOC AND FREE
  • 5BLOCKS OBjective-c For Java developers
  • //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);
  • //save a block in a variableint (^aBlock)(int, int) = ^(int i, int j) { return i * j; };//execute itint result = aBlock(1,2);5Keep it
  • //get the block as a parameter- (int)operationWithArg1:(int)i arg2:(int)j block:(int (^)(int, int))aBlock { return aBlock(i,j);}5Get it
  • “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 &
  • 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 MASTER though
  • 6Other stuff OBjective-c For Java developers
  • Localizable.strings ... "About" = "Sobre";NSString *localizedAbout = "Open" = "Abrir"; NSLocalizedString(@"About", nil); "Close" = "Fechar"; ...6クール!
  • - (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
  • 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 scare you !
  • 7DEMO OBjective-c For Java developers
  • Thanks for listeningI’m @fbbernardo on twitter and fbernardo on github Thanks to @hugojsfranca for the awesome design suggestions