23. Dot accessing property
[ instance setPoperty: 2];
is equal to
instance.property = 2;
12年7月5日星期四
24. self
• self like this in C++
- (id)init
{
self = [super init];
if (self) {
// Custom initialization
}
return self;
}
12年7月5日星期四
25. id
• something like void*
• useful at runtime
12年7月5日星期四
26. selector
• name of method , function literal
• compile time
SEL aSelector = @selector(methodName);
• run time
SEL aSelector = NSSelectorFromString(@"print");
NSString* selectorStr =
NSStringFromSelector(aSelector);
NSLog(@"%@", selectorStr);
12年7月5日星期四
27. selector
[friend performSelector:@selector(gossipAbout:)
withObject:aNeighbor];
is equal to
[friend gossipAbout:aNeighbor];
12年7月5日星期四
28. Runtime
library
彈性:原本在 compile
time, link time 要做的動
作,延到 run time 再做
12年7月5日星期四
29. object A
-(void)fly
完全沒有任何繼承關係
object B
-(void)fly
SEL userSelector = NSSelectorFromString(@”fly”);
[ any performSelector: @selector(userSelector) ];
any 指向 object A, 就呼叫 A 的 fly
any 指向 object B, 就呼叫 B 的 fly
12年7月5日星期四
30. Category
• extending class (even if you don’t have the
source)
#import "High.h"
@interface High (calculate)
- (NSInteger) plus:(NSInteger)operand1 and:(NSInteger)operand2;
@end
@implementation High (calculate)
- (NSInteger) plus:(NSInteger)operand1 and:(NSInteger)operand2
{
return operand1 + operand2;
}
@end
12年7月5日星期四
31. Class extension
• category without name
• used as private method declaration in Obj-
C
12年7月5日星期四
32. Category may cause
chaos
• category wins over the override function
• define same function in different
categories. Lead to undefined condition.
12年7月5日星期四
33. @protocol
• class with only methods.
• classes 之間說好要提供哪些 functions.
@protocol MyXMLSupport
- initFromXMLRepresentation:(NSXMLElement *)XMLElement;
- (NSXMLElement *)XMLRepresentation;
@end
12年7月5日星期四
34. memory management
• MRC - manual reference counting
• ARC - automatic reference counting
• garbage collection - in OS X, not in iOS
12年7月5日星期四
36. ARC
• supported on LLVM 3.0
• generate release code for you.
12年7月5日星期四
37. Blocks
this section is borrowed from WWDC 2011, sec. “block
and central dispatch”
* ^
12年7月5日星期四
38. Functions Blocks
// body of code // body of code
{ {
return a - b; return a - b;
} }
// pointer to a function // pointer to a block
* ^
12年7月5日星期四
39. Functions Blocks
// body of code // body of code
{ {
return a - b; return a - b;
} }
// pointer to a function // pointer to a block
int (*cmpr)(int, int); int (^cmpr)(int, int);
12年7月5日星期四
40. Functions Blocks
// body of code // body of code
{ {
return a - b; return a - b;
} }
// pointer to a function // pointer to a block
int (*cmpr)(int, int); int (^cmpr)(int, int);
typedef int (*func_t) typedef int (^block_t)
(int, int); (int, int);
12年7月5日星期四
41. Functions Blocks
// body of code // body of code
{ {
return a - b; return a - b;
} }
// pointer to a function // pointer to a block
int (*cmpr)(int, int); int (^cmpr)(int, int);
typedef int (*func_t) typedef int (^block_t)
(int, int); (int, int);
func_t cmpr = arg; block_t cmpr = arg;
12年7月5日星期四
42. Functions Blocks
// body of code // body of code
{ {
return a - b; return a - b;
} }
// pointer to a function // pointer to a block
int (*cmpr)(int, int); int (^cmpr)(int, int);
typedef int (*func_t) typedef int (^block_t)
(int, int); (int, int);
func_t cmpr = arg; block_t cmpr = arg;
cmpr(x, y); cmpr(x, y);
12年7月5日星期四
43. Functions Blocks
// body of code // body of code
{ {
return a - b; return a - b;
} }
// pointer to a function // pointer to a block
* ^
12年7月5日星期四
44. Functions Blocks
// body of code // body of code
int my_cmp(int a, int b) ^(int a, int b)
{ {
return a - b; return a - b;
} }
12年7月5日星期四
45. Functions Blocks
// body of code // body of code
int my_cmp(int a, int b) ^(int a, int b)
{ {
return a - b; return a - b;
} }
// usage // usage
sort(array, 10, my_cmp); sort(array, 10, ^(int a,
int b)
{
return a - b;
});
12年7月5日星期四
46. closure Blocks
// body of code
^(int a, int b)
{
return a - b;
}
12年7月5日星期四
47. closure Blocks
// body of code
__block int cnt = 0;
^(int a, int b)
{
cnt++ ;
return a - b;
}
log(“Count: %d”, cnt);
12年7月5日星期四