Objective-C for Java developers
-
Upload
fabio-bernardo -
Category
Technology
-
view
1.243 -
download
3
description
Transcript of Objective-C for Java developers
Objective-cFOR
JAVA DEVELOPERS
1OBjective-c For Java developers
The basics
1Hello world
System.out.println("Hello, World!"); NSLog(@"Hello, World!");
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];
1Methods
String aString = new String(chars);NSString *aString = [[NSString alloc] initWithUTF8String:chars];
1Methods
String aString = new String(chars);NSString *aString = [[NSString alloc] initWithUTF8String:chars];
1Methods
String aString = new String(chars);NSString *aString = [[NSString alloc] initWithUTF8String:chars];
1Methods
String aString = new String(chars);NSString *aString = [[NSString alloc] initWithUTF8String:chars];
1Methods
String aString = new String(chars);NSString *aString = [[NSString alloc] initWithUTF8String:chars];
thebasics
arethe
same!
Recap
thebasics
arethe
same!also, YAY
for unsigned
Recap
2OBjective-c For Java developers
Meet the objects
2Pointers
String s = new String(); NSString *s = [[NSString alloc] init];
2Pointers
String s = new String(); NSString *s = [[NSString alloc] init];
Stuff0x3DE2FE
2Pointers
String s = new String(); NSString *s = [[NSString alloc] init];
Stuff0x3DE2FE
2Pointers
String s = new String(); NSString *s = [[NSString alloc] init];
Stuff0x3DE2FE
2Pointers
String s = new String(); NSString *s = [[NSString alloc] init];
Stuff0x3DE2FE Object*
2Pointers
String s = new String(); NSString *s = [[NSString alloc] init];
Stuff0x3DE2FE
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)
2String
String aString = new String("A string.");NSString *aString = [[NSString alloc]
initWithString:@"A string."];
2String
String aString = new String("A string.");NSString *aString = [[NSString alloc]
initWithString:@"A string."];
2String
String aString = new String("A string.");NSString *aString = [[NSString alloc]
initWithString:@"A string."];
String aString = "A string."; NSString *aString = @"A string.";
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];
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];
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];
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];
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];
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];
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?
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 :(
2(NS)Array
String strings[] = {"1", "2"}; NSArray *strings = @[@"1", @"2"];
2(NS)Array
String strings[] = new String[10];NSString **strings = malloc(10*sizeof(NSString *));
List<String> strings = new ArrayList<String>(10);
NSMutableArray *strings = [NSMutableArray arrayWithCapacity:10];
2(NS)Array
2Dictionary
Map<Object, Object> m = new HashMap<Object,Object>();m.put("key","value");
NSDictionary *m = @{@"key" : @"value"};
2Dictionary
Map<Object, Object> m = new HashMap<Object,Object>();m.put("key","value");
NSMutableDictionary *m = [@{@"key" : @"value"} mutableCopy];[m setObject:@"key2" forKey:@"value2"];
2Dictionary
Map<Object, Object> m = new HashMap<Object,Object>();m.put("key","value");
NSMutableDictionary *m = [@{@"key" : @"value"} mutableCopy];[m setObject:@"key2" forKey:@"value2"];
2Dictionary
Map<Object, Object> m = new HashMap<Object,Object>();m.put("key","value");
NSMutableDictionary *m = [@{@"key" : @"value"} mutableCopy];[m setObject:@"key2" forKey:@"value2"];
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";
RecapPOINTER =OBJECT
LITERALS ARE AWESOME
INIT INITIALIZEALLOC CREATE
RecapPOINTER =OBJECT
STRINGCONCATSUCKS
LITERALS ARE AWESOME
INIT INITIALIZEALLOC CREATE
3OBjective-c For Java developers
Interfaces & implementations
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
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
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
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
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
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
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
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
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; }}
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)
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
3Categories
@interface NSString (MD5)
- (NSString *)md5Hash;
@end
@implementation NSString (MD5)
- (NSString *)md5Hash { NSString *md5 = anMD5Func(self); return md5;}
@end
.h .m
Recap
PROPERTIES ARE
AWESOME
IMPLEMENTATION .MInterface .H
Interfaces are called protocols and there’s
no abstract classes
Recap
PROPERTIES ARE
AWESOME
IMPLEMENTATION .MInterface .H
Interfaces are called protocols and there’s
no abstract classesAnd
imagine what you
can do with
categories
4OBjective-c For Java developers
remember c?
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
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
4The Object
typedef struct objc_object { Class isa;} *id;
typedef struct objc_class *Class;
objc.h
struct objc_class { Class isa;};
runtime.h
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.”
RecapC Is at the bottom of
objective-c and YOU
SHOULD TAKE ADVANTAGE OF
IT
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
5OBjective-c For Java developers
BLOCKS
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);
5Keep it
//save a block in a variableint (^aBlock)(int, int) = ^(int i, int j) { return i * j; };
//execute itint result = aBlock(1,2);
5Get it
//get the block as a parameter- (int)operationWithArg1:(int)i arg2:(int)j block:(int (^)(int, int))aBlock { return aBlock(i,j);}
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.”
RecapBLOCKS Is MY
FAVORITE OBJECTIVE-C
LIBRARY AND IT WILL BE ONE OF
YOURS TOO
RecapBLOCKS Is MY
FAVORITE OBJECTIVE-C
LIBRARY AND IT WILL BE ONE OF
YOURS TOO
The notation is hard to
MASTER though
6OBjective-c For Java developers
Other stuff
6クール!NSString *localizedAbout = NSLocalizedString(@"About", nil);
..."About" = "Sobre";"Open" = "Abrir";"Close" = "Fechar";...
Localizable.strings
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);
Recap
this is just the beginning.
There’s a lot more to
objective-c
Recap
this is just the beginning.
There’s a lot more to
objective-c But now it won’t
scare you !
7OBjective-c For Java developers
DEMO
!"#$% f&r
'(%)*#(#+!"#$% )& @,-+&.%fr"#/" f&r ),* "w*%&0* 1*%(+# %-++*%)(&#%
I’0 @fbb*r#"r1& &# )w())*r "#1 fb*r#"r1& &# +(),-b