+ All Categories
Home > Documents > Objc under the_hood_2013

Objc under the_hood_2013

Date post: 28-Jun-2015
Category:
Upload: michael-pan
View: 966 times
Download: 7 times
Share this document with a friend
Popular Tags:
70
Objective-C - Under the hood Michael Pan 13521星期
Transcript
Page 1: Objc under the_hood_2013

Objective-C - Under the hoodMichael Pan

13年5月21⽇日星期⼆二

Page 2: Objc under the_hood_2013

Outline• Pointer & Object

• C Struct for class

• Dynamic feature

13年5月21⽇日星期⼆二

Page 3: Objc under the_hood_2013

ContactsE-mail : [email protected]

Facebook Group : Developer’s Lesson

http://www.facebook.com/groups/developerslesson

Facebook Page : Developer’s Note

http://www.facebook.com/pages/Taipei-Taiwan/Developers-note/226724001803

Blogger : Developer’s Note

http://iosdevelopersnote.blogspot.com/

13年5月21⽇日星期⼆二

Page 5: Objc under the_hood_2013

Pointer & Object

13年5月21⽇日星期⼆二

Page 6: Objc under the_hood_2013

Lets go from C

0x01

0x05

0x09

13年5月21⽇日星期⼆二

Page 7: Objc under the_hood_2013

Lets go from C

int a;0x01

0x05

0x09

13年5月21⽇日星期⼆二

Page 8: Objc under the_hood_2013

Lets go from C

int a;a 0x01

0x05

0x09

13年5月21⽇日星期⼆二

Page 9: Objc under the_hood_2013

Lets go from C

int a;a

a=5;

0x01

0x05

0x09

13年5月21⽇日星期⼆二

Page 10: Objc under the_hood_2013

Lets go from C

int a;a

a=5;

50x01

0x05

0x09

13年5月21⽇日星期⼆二

Page 11: Objc under the_hood_2013

Pointer

int a;

a=5;

a 50x01

0x09

0x05

& => address of

13年5月21⽇日星期⼆二

Page 12: Objc under the_hood_2013

Pointer

int a;

a=5;

int * pa;

a 50x01

0x09

0x05

& => address of

13年5月21⽇日星期⼆二

Page 13: Objc under the_hood_2013

Pointer

int a;

a=5;

int * pa;

pa

a 50x01

0x09

0x05

& => address of

13年5月21⽇日星期⼆二

Page 14: Objc under the_hood_2013

Pointer

int a;

a=5;

int * pa;

pa

a 50x01

0x09

0x05

pa = &a;

& => address of

13年5月21⽇日星期⼆二

Page 15: Objc under the_hood_2013

Pointer

int a;

a=5;

int * pa;

pa

a 50x01

0x09

0x05

pa = &a;0x01

& => address of

13年5月21⽇日星期⼆二

Page 16: Objc under the_hood_2013

Pointer - More

int a;

a=5;

int * pa;

pa

a 50x01

0x09

0x05

pa = &a;0x01

printf(“%d”,*pa);

13年5月21⽇日星期⼆二

Page 17: Objc under the_hood_2013

Recap - pointerWrite some thing

13年5月21⽇日星期⼆二

Page 18: Objc under the_hood_2013

Pointer - More

int a;

a=5;

int * pa;

pa

a 50x01

0x09

0x05

pa = &a;0x01

13年5月21⽇日星期⼆二

Page 19: Objc under the_hood_2013

Pointer - More

int a;

a=5;

int * pa;

pa

a 50x01

0x09

0x05

pa = &a;0x01

*pa = 10

13年5月21⽇日星期⼆二

Page 20: Objc under the_hood_2013

Pointer - More

int a;

a=5;

int * pa;

pa

a 50x01

0x09

0x05

pa = &a;0x01

*pa = 10

printf(“%d”,a);13年5月21⽇日星期⼆二

Page 21: Objc under the_hood_2013

Pointer - More

int a;

a=5;

int * pa;

pa

a 0x01

0x09

0x05

pa = &a;0x01

*pa = 10

printf(“%d”,a);

10

13年5月21⽇日星期⼆二

Page 22: Objc under the_hood_2013

Struct• More complicated type

struct Date { int day; int month; int year;};

struct Date date = {3,10,1970}; printf("The day is %d, %d/%d", date.year, date.day, date.month);

13年5月21⽇日星期⼆二

Page 23: Objc under the_hood_2013

Struct - Memory

0x10date

struct Date { int day; int month; int year;};

13年5月21⽇日星期⼆二

Page 24: Objc under the_hood_2013

Struct - Memory

0x10date

struct Date date ;

struct Date { int day; int month; int year;};

13年5月21⽇日星期⼆二

Page 25: Objc under the_hood_2013

Struct - Memory

0x10date

struct Date date ;

struct Date { int day; int month; int year;};

13年5月21⽇日星期⼆二

Page 26: Objc under the_hood_2013

Struct - Memory

0x10date

struct Date date ;

date = {3,10,1970};

struct Date { int day; int month; int year;};

13年5月21⽇日星期⼆二

Page 27: Objc under the_hood_2013

Struct - Memory

0x10date

struct Date date ;

date = {3,10,1970};

struct Date { int day; int month; int year;};

3

10

1970

13年5月21⽇日星期⼆二

Page 28: Objc under the_hood_2013

Function• we know main function

• make another function

13年5月21⽇日星期⼆二

Page 29: Objc under the_hood_2013

Function - Memory

int add(int a, int b){ int result = a+b; return result;}

add

result

in Stack

13年5月21⽇日星期⼆二

Page 30: Objc under the_hood_2013

• Text Segment - 唯讀執⾏行區• Data Segment - 可讀寫區包含 global 變數• Heap - 可依程式需要產⽣生和消除記憶體(動態配置- malloc)

• Stack - 為了 function 產⽣生,可變動⼤大⼩小,包含區域變數

Objective-C 程式記憶體配置

Text Segment

Data Segment

Heap

Stack

未使⽤用

記憶體位置增加

Stack Pointer

13年5月21⽇日星期⼆二

Page 31: Objc under the_hood_2013

Stack 與 Function

Text Segment

Data Segment

Heap

StackStack Pointer

int main (int argc, const char * argv[]) { // ... 省略

!}

13年5月21⽇日星期⼆二

Page 32: Objc under the_hood_2013

Stack 與 Function

Text Segment

Data Segment

Heap

StackStack Pointer

int main (int argc, const char * argv[]) { // ... 省略

!}

int sum = add(5, 6);

13年5月21⽇日星期⼆二

Page 33: Objc under the_hood_2013

Stack 與 Function

Text Segment

Data Segment

Heap

StackStack Pointer

int main (int argc, const char * argv[]) { // ... 省略

!}

int sum = add(5, 6);

13年5月21⽇日星期⼆二

Page 34: Objc under the_hood_2013

Stack 與 Function

Text Segment

Data Segment

Heap

Stack

Stack Pointer

int main (int argc, const char * argv[]) { // ... 省略

!}

int sum = add(5, 6);

13年5月21⽇日星期⼆二

Page 35: Objc under the_hood_2013

Stack 與 Function

Text Segment

Data Segment

Heap

Stack

Stack Pointer

int main (int argc, const char * argv[]) { // ... 省略

!}

int sum = add(5, 6);

// 下⼀一⾏行程式碼

13年5月21⽇日星期⼆二

Page 36: Objc under the_hood_2013

Stack 與 Function

Text Segment

Data Segment

Heap

Stack

Stack Pointer

int main (int argc, const char * argv[]) { // ... 省略

!}

int sum = add(5, 6);

// 下⼀一⾏行程式碼

13年5月21⽇日星期⼆二

Page 37: Objc under the_hood_2013

Stack 與 Function

Text Segment

Data Segment

Heap

StackStack Pointer

int main (int argc, const char * argv[]) { // ... 省略

!}

int sum = add(5, 6);

// 下⼀一⾏行程式碼

13年5月21⽇日星期⼆二

Page 38: Objc under the_hood_2013

Stack 與 Heap

Text Segment

Data Segment

Heap

Stack local variable

malloc

13年5月21⽇日星期⼆二

Page 39: Objc under the_hood_2013

Pointer - 動態配置記憶體

int * p_a;p_a = malloc(sizeof(int));*p_a = 8;printf("value in pointer a is %d\n", *p_a);

#include <stdlib.h>

p_a

Heap

8

Stack

0xA0

0xA0

13年5月21⽇日星期⼆二

Page 40: Objc under the_hood_2013

Static variable• in Data Segment

Text Segment

Heap

Stack

未使⽤用

Data Segment

int callStaticValue();

int main (int argc, const char * argv[]) {callStaticValue();callStaticValue();printf("static variable is %d", callStaticValue());return 0;

}

int callStaticValue(){! static int sVar=0; // static 變數初始化只會做⼀一次! return sVar++; // ⼀一直加1}

13年5月21⽇日星期⼆二

Page 41: Objc under the_hood_2013

Struct + function• it is what we call Object

struct Date {int day;

int month; int year;void (*formatedDate)( struct Date date);

};

void formatedFunction(struct Date date){ printf("The day is %d, %d/%d",date.year, date.month, date.day );}

// in main()struct Date today = {29, 4, 2013};today.formatedDate = formatedFunction;today.formatedDate(today);

13年5月21⽇日星期⼆二

Page 42: Objc under the_hood_2013

Objective-C class - C struct• /usr/include/objc/objc.htypedef struct objc_class *Class;typedef struct objc_object { Class isa;} *id;

typedef struct objc_selector !*SEL;

typedef id (*IMP)(id, SEL, ...);

13年5月21⽇日星期⼆二

Page 43: Objc under the_hood_2013

SELstruct objc_selector{ void *sel_id; const char *sel_types;};

13年5月21⽇日星期⼆二

Page 44: Objc under the_hood_2013

Objective-C class - C struct• /usr/include/objc/runtime.hstruct objc_class { Class isa;

#if !__OBJC2__ Class super_class OBJC2_UNAVAILABLE; const char *name OBJC2_UNAVAILABLE; long version OBJC2_UNAVAILABLE; long info OBJC2_UNAVAILABLE; long instance_size OBJC2_UNAVAILABLE; struct objc_ivar_list *ivars OBJC2_UNAVAILABLE; struct objc_method_list **methodLists OBJC2_UNAVAILABLE; /* ignore */#endif

} OBJC2_UNAVAILABLE;/* Use `Class` instead of `struct objc_class *` *//* ignore */ typedef struct { const char *name; const char *value;} objc_property_attribute_t;

13年5月21⽇日星期⼆二

Page 45: Objc under the_hood_2013

Methodstruct objc_method { SEL method_name OBJC2_UNAVAILABLE; char *method_types OBJC2_UNAVAILABLE; IMP method_imp OBJC2_UNAVAILABLE;} OBJC2_UNAVAILABLE;

struct objc_method_list { struct objc_method_list *obsolete OBJC2_UNAVAILABLE;

int method_count OBJC2_UNAVAILABLE;#ifdef __LP64__ int space OBJC2_UNAVAILABLE;#endif /* variable length structure */ struct objc_method method_list[1] OBJC2_UNAVAILABLE;} OBJC2_UNAVAILABLE;

13年5月21⽇日星期⼆二

Page 46: Objc under the_hood_2013

About Message• Add method runtime

13年5月21⽇日星期⼆二

Page 47: Objc under the_hood_2013

Add method runtimestruct objc_method {  SEL method_name;  char *method_types;  IMP method_imp;};

struct objc_method_list {  struct objc_method_list *obsolete;  int method_count;  struct objc_method method_list[1];};

13年5月21⽇日星期⼆二

Page 48: Objc under the_hood_2013

Add method#import <objc/objc-class.h>

// create a class with no methods@interface EmptyClass : NSObject { }@end

@implementation EmptyClass@end

// define the function to add as a methodid sayHello ( id self, SEL _cmd,... ){  NSLog (@"Hello");}

void addMethod (){  // create the method

  struct objc_method myMethod;  myMethod.method_name = sel_registerName("sayHello");  myMethod.method_imp  = sayHello;    // build the method list.  // this memory needs to stick around as long as the  // methods belong to the class.

  struct objc_method_list * myMethodList;  myMethodList = malloc (sizeof(struct objc_method_list));  myMethodList->method_count = 1;  myMethodList->method_list[0] = myMethod;    // add method to the class  class_addMethods ( [EmptyClass class], myMethodList );    // try it out  EmptyClass * instance = [[EmptyClass alloc] init];  [instance sayHello];  [instance release];}

13年5月21⽇日星期⼆二

Page 49: Objc under the_hood_2013

Method under the hood

MyClass * aObj;int para = 5;[aObj setCount:para];

objc_msgSend( aObj, @selector( setCount: ), para );

selector function

setCount: ...

count ...

經由 isa 找到 Class structure

從table 找到相對應的 function

13年5月21⽇日星期⼆二

Page 50: Objc under the_hood_2013

Demo• DemoNCCU13 - MyCObject

13年5月21⽇日星期⼆二

Page 51: Objc under the_hood_2013

Dynamic feature • Dot operation

• KVC

13年5月21⽇日星期⼆二

Page 52: Objc under the_hood_2013

Convenient Way- Since Objective-C 2.0

- Dot Syntax

- Cascade

NSString *name = person.name; // name = [person name]

person.name = @”Michael”; // [person setName:@”Michael”]

person.bill.name = @”Andy”; // [[person bill] setName:@”Andy”] person.bill.name ; // [[person bill] name]

13年5月21⽇日星期⼆二

Page 53: Objc under the_hood_2013

Property means method@interface Person : NSObject

@property int age;@property (strong) NSString * name;

@end

@implementation Person@end

Person * person = [[Person alloc] init];person.name = @”Michael”;person.age = 35;

13年5月21⽇日星期⼆二

Page 54: Objc under the_hood_2013

Key-Value Coding

13年5月21⽇日星期⼆二

Page 55: Objc under the_hood_2013

Another access method• Read

• person.name;

• [person name];

• [person valueForKey:@”name”];

• Write

• person.name = @”michael”;

• [person setName:@”michael”];

• [person setValue:@”michael” forKey:@”name”];

13年5月21⽇日星期⼆二

Page 56: Objc under the_hood_2013

• Find the action -name or -setName

• Find real variable _name and then name

Finding rule

forKey:@”name”

13年5月21⽇日星期⼆二

Page 57: Objc under the_hood_2013

• attribute

• scalar // auto-boxing for KVC

• NSString

• Boolean

• Immutable object : NSColor, NSNumber

• to-many relationship - objects in NSArray, NSSet

Accessible Types

13年5月21⽇日星期⼆二

Page 58: Objc under the_hood_2013

Recap@interface MyObject : NSObject {! NSString * name;! NSString * _name;}

@property (retain, readonly) NSString * _name;@property (retain, readonly) NSString * name;@end

MyObject * myo = [MyObject new];[myo setValue:@"michael" forKey:@"name"];NSLog(@" kvc %@",[myo valueForKey:@"name"]);

Result : kvc (null) ? 0. no setter1. find out _name = @”michael”2. find out name’s getter, return name

@implementation MyObject@synthesize name, _name;

@end

下底線開頭的變數要⼩小⼼心使⽤用13年5月21⽇日星期⼆二

Page 59: Objc under the_hood_2013

[myCar valueForKeyPath: @"engine.vendor"]

KeyPath

@interface MyCar : NSObject {! Engine * engine;}

@end

@interface Engine : NSObject {! NSString * vendor;}

@end

13年5月21⽇日星期⼆二

Page 60: Objc under the_hood_2013

NSNumber * count;

count = [myCars valueForKeyPath: @"@avg.price"]

Array Operation

@interface MyCar : NSObject {! int price;}

@end

@[email protected]@max.xxx

myCar1 myCar2 myCar3myCars =

13年5月21⽇日星期⼆二

Page 61: Objc under the_hood_2013

Recap

// 計算 engines array裡平均的priceNSArray * engines = [???:myengine1, myengine2, myengine3, nil];NSLog(@" average price is %@",[engines valueForKeyPath:@"???"]);

@interface Engine : NSObject {! int price;}

@end

13年5月21⽇日星期⼆二

Page 62: Objc under the_hood_2013

nil for scalar value ?

[engine setValue: nil forKey:@”price”]

error !!

- (void) setNilValueForKey: (NSString *) key { if ([key isEqualToString: @"price"]) { price = 0; } else { [super setNilValueForKey: key]; } }

overwrite -setNilValueForKey:

13年5月21⽇日星期⼆二

Page 63: Objc under the_hood_2013

Handle UndefinedKey

- (void) setValue: (id) value forUndefinedKey: (NSString *) key { // do something }

- (id) valueForUndefinedKey:(NSString *)key { // do something }

13年5月21⽇日星期⼆二

Page 64: Objc under the_hood_2013

Recap

實作- (void) setValue: (id) value forUndefinedKey: (NSString *) key;

- (id) valueForUndefinedKey:(NSString *)key;

13年5月21⽇日星期⼆二

Page 65: Objc under the_hood_2013

Reference

Introduction to Key-Value Coding Programming Guide

http://goo.gl/rrfmy

13年5月21⽇日星期⼆二

Page 66: Objc under the_hood_2013

實例 - 解析 RSS

網路上免費資源 - 多⽤用來呈現新聞

13年5月21⽇日星期⼆二

Page 67: Objc under the_hood_2013

With XML - RSS-like<car> <item>

<price> 99 </price> <year> 1922 </year>

</item> <item>

<price> 30 </price> <year> 1990 </year>

</item> <item>

<price> 82 </price> <year> 1980 </year>

</item> <item>

<price> 75 </price> <year> 1920 </year>

</item> <item>

<price> 60 </price> <year> 1910 </year>

</item></car>

Array

Dictionary

key value

price 99

year 1922

13年5月21⽇日星期⼆二

Page 68: Objc under the_hood_2013

Array and KVC<car> <item>

<price> 99 </price> <year> 1922 </year>

</item> <item>

<price> 30 </price> <year> 1990 </year>

</item> <item>

<price> 82 </price> <year> 1980 </year>

</item> <item>

<price> 75 </price> <year> 1920 </year>

</item> <item>

<price> 60 </price> <year> 1910 </year>

</item></car>

Array

Dictionary

[array valueForKey:@”price”]

(99,30,82,75,60,

)

13年5月21⽇日星期⼆二

Page 69: Objc under the_hood_2013

DemoValueAndPredicate

13年5月21⽇日星期⼆二

Page 70: Objc under the_hood_2013

Question

13年5月21⽇日星期⼆二


Recommended