SlideShare a Scribd company logo
1 of 70
Download to read offline
Objective-C - Under the hood
Michael Pan
13年5月21⽇日星期⼆二
Outline
• Pointer & Object
• C Struct for class
• Dynamic feature
13年5月21⽇日星期⼆二
Contacts
E-mail : scentsome@gmail.com
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⽇日星期⼆二
Book
Objective-C 與 iOS 開發⼊入⾨門
http://www.books.com.tw/exep/prod/
booksfile.php?item=0010517912
13年5月21⽇日星期⼆二
Pointer & Object
13年5月21⽇日星期⼆二
Lets go from C
0x01
0x05
0x09
13年5月21⽇日星期⼆二
Lets go from C
int a;0x01
0x05
0x09
13年5月21⽇日星期⼆二
Lets go from C
int a;a 0x01
0x05
0x09
13年5月21⽇日星期⼆二
Lets go from C
int a;a
a=5;
0x01
0x05
0x09
13年5月21⽇日星期⼆二
Lets go from C
int a;a
a=5;
50x01
0x05
0x09
13年5月21⽇日星期⼆二
Pointer
int a;
a=5;
a 50x01
0x09
0x05
& => address of
13年5月21⽇日星期⼆二
Pointer
int a;
a=5;
int * pa;
a 50x01
0x09
0x05
& => address of
13年5月21⽇日星期⼆二
Pointer
int a;
a=5;
int * pa;
pa
a 50x01
0x09
0x05
& => address of
13年5月21⽇日星期⼆二
Pointer
int a;
a=5;
int * pa;
pa
a 50x01
0x09
0x05
pa = &a;
& => address of
13年5月21⽇日星期⼆二
Pointer
int a;
a=5;
int * pa;
pa
a 50x01
0x09
0x05
pa = &a;
0x01
& => address of
13年5月21⽇日星期⼆二
Pointer - More
int a;
a=5;
int * pa;
pa
a 50x01
0x09
0x05
pa = &a;
0x01
printf(“%d”,*pa);
13年5月21⽇日星期⼆二
Recap - pointer
Write some thing
13年5月21⽇日星期⼆二
Pointer - More
int a;
a=5;
int * pa;
pa
a 50x01
0x09
0x05
pa = &a;
0x01
13年5月21⽇日星期⼆二
Pointer - More
int a;
a=5;
int * pa;
pa
a 50x01
0x09
0x05
pa = &a;
0x01
*pa = 10
13年5月21⽇日星期⼆二
Pointer - More
int a;
a=5;
int * pa;
pa
a 50x01
0x09
0x05
pa = &a;
0x01
*pa = 10
printf(“%d”,a);
13年5月21⽇日星期⼆二
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⽇日星期⼆二
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⽇日星期⼆二
Struct - Memory
0x10date
struct Date {
int day;
int month;
int year;
};
13年5月21⽇日星期⼆二
Struct - Memory
0x10date
struct Date date ;
struct Date {
int day;
int month;
int year;
};
13年5月21⽇日星期⼆二
Struct - Memory
0x10date
struct Date date ;
struct Date {
int day;
int month;
int year;
};
13年5月21⽇日星期⼆二
Struct - Memory
0x10date
struct Date date ;
date = {3,10,1970};
struct Date {
int day;
int month;
int year;
};
13年5月21⽇日星期⼆二
Struct - Memory
0x10date
struct Date date ;
date = {3,10,1970};
struct Date {
int day;
int month;
int year;
};
3
10
1970
13年5月21⽇日星期⼆二
Function
• we know main function
• make another function
13年5月21⽇日星期⼆二
Function - Memory
int add(int a, int b){
int result = a+b;
return result;
}
add
result
in Stack
13年5月21⽇日星期⼆二
• Text Segment - 唯讀執⾏行區
• Data Segment - 可讀寫區包含 global 變數
• Heap - 可依程式需要產⽣生和消除記憶體(動態配置- malloc)
• Stack - 為了 function 產⽣生,可變動⼤大⼩小,包含區域變數
Objective-C 程式記憶體配置
Text Segment
Data Segment
Heap
Stack
未使⽤用
記憶體位置增加
Stack Pointer
13年5月21⽇日星期⼆二
Stack 與 Function
Text Segment
Data Segment
Heap
Stack
Stack Pointer
int main (int argc, const char * argv[]) {
// ... 省略
!
}
13年5月21⽇日星期⼆二
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⽇日星期⼆二
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⽇日星期⼆二
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⽇日星期⼆二
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⽇日星期⼆二
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⽇日星期⼆二
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⽇日星期⼆二
Stack 與 Heap
Text Segment
Data Segment
Heap
Stack local variable
malloc
13年5月21⽇日星期⼆二
Pointer - 動態配置記憶體
int * p_a;
p_a = malloc(sizeof(int));
*p_a = 8;
printf("value in pointer a is %dn", *p_a);
#include <stdlib.h>
p_a
Heap
8
Stack
0xA0
0xA0
13年5月21⽇日星期⼆二
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⽇日星期⼆二
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⽇日星期⼆二
Objective-C class - C struct
• /usr/include/objc/objc.h
typedef struct objc_class *Class;
typedef struct objc_object {
Class isa;
} *id;
typedef struct objc_selector !*SEL;
typedef id (*IMP)(id, SEL, ...);
13年5月21⽇日星期⼆二
SEL
struct objc_selector
{
void *sel_id;
const char *sel_types;
};
13年5月21⽇日星期⼆二
Objective-C class - C struct
• /usr/include/objc/runtime.h
struct 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⽇日星期⼆二
Method
struct 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⽇日星期⼆二
About Message
• Add method runtime
13年5月21⽇日星期⼆二
Add method runtime
struct 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⽇日星期⼆二
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 method
id 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⽇日星期⼆二
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⽇日星期⼆二
Demo
• DemoNCCU13 - MyCObject
13年5月21⽇日星期⼆二
Dynamic feature
• Dot operation
• KVC
13年5月21⽇日星期⼆二
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⽇日星期⼆二
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⽇日星期⼆二
Key-Value Coding
13年5月21⽇日星期⼆二
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⽇日星期⼆二
• Find the action -name or -setName
• Find real variable _name and then name
Finding rule
forKey:@”name”
13年5月21⽇日星期⼆二
• attribute
• scalar // auto-boxing for KVC
• NSString
• Boolean
• Immutable object : NSColor, NSNumber
• to-many relationship - objects in NSArray, NSSet
Accessible Types
13年5月21⽇日星期⼆二
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 setter
1. find out _name = @”michael”
2. find out name’s getter, return name
@implementation MyObject
@synthesize name, _name;
@end
下底線開頭的變數要⼩小⼼心使⽤用
13年5月21⽇日星期⼆二
[myCar valueForKeyPath: @"engine.vendor"]
KeyPath
@interface MyCar : NSObject {
! Engine * engine;
}
@end
@interface Engine : NSObject {
! NSString * vendor;
}
@end
13年5月21⽇日星期⼆二
NSNumber * count;
count = [myCars valueForKeyPath: @"@avg.price"]
Array Operation
@interface MyCar : NSObject {
! int price;
}
@end
@sum.xxx
@min.xxx
@max.xxx
myCar1 myCar2 myCar3myCars =
13年5月21⽇日星期⼆二
Recap
// 計算 engines array裡平均的price
NSArray * engines = [???:myengine1, myengine2, myengine3, nil];
NSLog(@" average price is %@",[engines valueForKeyPath:@"???"]);
@interface Engine : NSObject {
! int price;
}
@end
13年5月21⽇日星期⼆二
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⽇日星期⼆二
Handle UndefinedKey
- (void) setValue: (id) value forUndefinedKey: (NSString *) key {
// do something
}
- (id) valueForUndefinedKey:(NSString *)key {
// do something
}
13年5月21⽇日星期⼆二
Recap
實作
- (void) setValue: (id) value forUndefinedKey: (NSString *) key;
- (id) valueForUndefinedKey:(NSString *)key;
13年5月21⽇日星期⼆二
Reference
Introduction to Key-Value Coding Programming Guide
http://goo.gl/rrfmy
13年5月21⽇日星期⼆二
實例 - 解析 RSS
網路上免費資源 - 多⽤用來呈現新聞
13年5月21⽇日星期⼆二
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⽇日星期⼆二
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⽇日星期⼆二
Demo
ValueAndPredicate
13年5月21⽇日星期⼆二
Question
13年5月21⽇日星期⼆二

More Related Content

Viewers also liked

Autorelease pool
Autorelease poolAutorelease pool
Autorelease poolMichael Pan
 
Core data lightweight_migration
Core data lightweight_migrationCore data lightweight_migration
Core data lightweight_migrationMichael Pan
 
Homework2 play cards
Homework2 play cardsHomework2 play cards
Homework2 play cardsMichael Pan
 
比價撿便宜 Steven
比價撿便宜 Steven比價撿便宜 Steven
比價撿便宜 StevenMichael Pan
 
Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Michael Pan
 
Prototype by Xcode
Prototype by XcodePrototype by Xcode
Prototype by XcodeMichael Pan
 
Strategy Pattern for Objective-C
Strategy Pattern for Objective-CStrategy Pattern for Objective-C
Strategy Pattern for Objective-CMichael Pan
 
Eclipse and Genymotion
Eclipse and GenymotionEclipse and Genymotion
Eclipse and GenymotionMichael Pan
 
C programming structure & pointer
C  programming structure & pointerC  programming structure & pointer
C programming structure & pointerargusacademy
 
Introduction to Android Studio
Introduction to Android StudioIntroduction to Android Studio
Introduction to Android StudioMichael Pan
 

Viewers also liked (17)

Autorelease pool
Autorelease poolAutorelease pool
Autorelease pool
 
Core data lightweight_migration
Core data lightweight_migrationCore data lightweight_migration
Core data lightweight_migration
 
Homework2 play cards
Homework2 play cardsHomework2 play cards
Homework2 play cards
 
比價撿便宜 Steven
比價撿便宜 Steven比價撿便宜 Steven
比價撿便宜 Steven
 
Dropbox sync
Dropbox syncDropbox sync
Dropbox sync
 
Note something
Note somethingNote something
Note something
 
Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4
 
Activity
ActivityActivity
Activity
 
Prototype by Xcode
Prototype by XcodePrototype by Xcode
Prototype by Xcode
 
Shootting Game
Shootting GameShootting Game
Shootting Game
 
Strategy Pattern for Objective-C
Strategy Pattern for Objective-CStrategy Pattern for Objective-C
Strategy Pattern for Objective-C
 
Eclipse and Genymotion
Eclipse and GenymotionEclipse and Genymotion
Eclipse and Genymotion
 
C programming structure & pointer
C  programming structure & pointerC  programming structure & pointer
C programming structure & pointer
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 
Introduction to Android Studio
Introduction to Android StudioIntroduction to Android Studio
Introduction to Android Studio
 
Data types
Data typesData types
Data types
 
Programming in c
Programming in cProgramming in c
Programming in c
 

Similar to Objc under the_hood_2013

iOs app 101
iOs app 101iOs app 101
iOs app 101Tom Sun
 
iPhone,ios,Object-c基础入门
iPhone,ios,Object-c基础入门iPhone,ios,Object-c基础入门
iPhone,ios,Object-c基础入门Lucien Li
 
iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门Lucien Li
 
旺铺前端设计和实现
旺铺前端设计和实现旺铺前端设计和实现
旺铺前端设计和实现hua qiu
 
基于YUI3的组件开发
基于YUI3的组件开发基于YUI3的组件开发
基于YUI3的组件开发jay li
 
Network and Multitasking
Network and MultitaskingNetwork and Multitasking
Network and Multitaskingyarshure Kong
 
淺談C#物件導向與DesignPattern.pdf
淺談C#物件導向與DesignPattern.pdf淺談C#物件導向與DesignPattern.pdf
淺談C#物件導向與DesignPattern.pdfBrian Chou 周家禾
 
Django sharing
Django sharingDjango sharing
Django sharingTom Chen
 
FIM Media Player - BoxModel in Flash
FIM Media Player - BoxModel in FlashFIM Media Player - BoxModel in Flash
FIM Media Player - BoxModel in Flashqizhi20
 
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
炎炎夏日學 Android 課程 -  Part1: Kotlin 語法介紹炎炎夏日學 Android 課程 -  Part1: Kotlin 語法介紹
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹Johnny Sung
 
Kissy design
Kissy designKissy design
Kissy designyiming he
 
jQuery底层架构
jQuery底层架构jQuery底层架构
jQuery底层架构fangdeng
 
Java23种设计模式(总结)
Java23种设计模式(总结)Java23种设计模式(总结)
Java23种设计模式(总结)xuanlong282
 
YUI ─ 阿大
YUI ─ 阿大YUI ─ 阿大
YUI ─ 阿大taobao.com
 
组件交互模式的非主流研究
组件交互模式的非主流研究组件交互模式的非主流研究
组件交互模式的非主流研究youalab
 
5, initialization & cleanup
5, initialization & cleanup5, initialization & cleanup
5, initialization & cleanupted-xu
 

Similar to Objc under the_hood_2013 (20)

iOs app 101
iOs app 101iOs app 101
iOs app 101
 
iPhone,ios,Object-c基础入门
iPhone,ios,Object-c基础入门iPhone,ios,Object-c基础入门
iPhone,ios,Object-c基础入门
 
iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门
 
旺铺前端设计和实现
旺铺前端设计和实现旺铺前端设计和实现
旺铺前端设计和实现
 
ios分享
ios分享ios分享
ios分享
 
Web base 吴志华
Web base 吴志华Web base 吴志华
Web base 吴志华
 
基于YUI3的组件开发
基于YUI3的组件开发基于YUI3的组件开发
基于YUI3的组件开发
 
Network and Multitasking
Network and MultitaskingNetwork and Multitasking
Network and Multitasking
 
淺談C#物件導向與DesignPattern.pdf
淺談C#物件導向與DesignPattern.pdf淺談C#物件導向與DesignPattern.pdf
淺談C#物件導向與DesignPattern.pdf
 
Js dom
Js domJs dom
Js dom
 
C語言結構與串列
C語言結構與串列 C語言結構與串列
C語言結構與串列
 
Django sharing
Django sharingDjango sharing
Django sharing
 
FIM Media Player - BoxModel in Flash
FIM Media Player - BoxModel in FlashFIM Media Player - BoxModel in Flash
FIM Media Player - BoxModel in Flash
 
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
炎炎夏日學 Android 課程 -  Part1: Kotlin 語法介紹炎炎夏日學 Android 課程 -  Part1: Kotlin 語法介紹
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
 
Kissy design
Kissy designKissy design
Kissy design
 
jQuery底层架构
jQuery底层架构jQuery底层架构
jQuery底层架构
 
Java23种设计模式(总结)
Java23种设计模式(总结)Java23种设计模式(总结)
Java23种设计模式(总结)
 
YUI ─ 阿大
YUI ─ 阿大YUI ─ 阿大
YUI ─ 阿大
 
组件交互模式的非主流研究
组件交互模式的非主流研究组件交互模式的非主流研究
组件交互模式的非主流研究
 
5, initialization & cleanup
5, initialization & cleanup5, initialization & cleanup
5, initialization & cleanup
 

More from Michael Pan

Superstar dj pdf
Superstar dj pdfSuperstar dj pdf
Superstar dj pdfMichael Pan
 
Appsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperAppsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperMichael Pan
 
GZFox Inc. Jacky
GZFox Inc. JackyGZFox Inc. Jacky
GZFox Inc. JackyMichael Pan
 
創投公司 hoku_20121017
創投公司 hoku_20121017創投公司 hoku_20121017
創投公司 hoku_20121017Michael Pan
 
Opening iOS App 開發者交流會
Opening iOS App 開發者交流會Opening iOS App 開發者交流會
Opening iOS App 開發者交流會Michael Pan
 

More from Michael Pan (7)

Nimbus
NimbusNimbus
Nimbus
 
Superstar dj pdf
Superstar dj pdfSuperstar dj pdf
Superstar dj pdf
 
ADB - Arthur
ADB - ArthurADB - Arthur
ADB - Arthur
 
Appsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperAppsgaga - iOS Game Developer
Appsgaga - iOS Game Developer
 
GZFox Inc. Jacky
GZFox Inc. JackyGZFox Inc. Jacky
GZFox Inc. Jacky
 
創投公司 hoku_20121017
創投公司 hoku_20121017創投公司 hoku_20121017
創投公司 hoku_20121017
 
Opening iOS App 開發者交流會
Opening iOS App 開發者交流會Opening iOS App 開發者交流會
Opening iOS App 開發者交流會
 

Objc under the_hood_2013

  • 1. Objective-C - Under the hood Michael Pan 13年5月21⽇日星期⼆二
  • 2. Outline • Pointer & Object • C Struct for class • Dynamic feature 13年5月21⽇日星期⼆二
  • 3. Contacts E-mail : scentsome@gmail.com 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⽇日星期⼆二
  • 4. Book Objective-C 與 iOS 開發⼊入⾨門 http://www.books.com.tw/exep/prod/ booksfile.php?item=0010517912 13年5月21⽇日星期⼆二
  • 6. Lets go from C 0x01 0x05 0x09 13年5月21⽇日星期⼆二
  • 7. Lets go from C int a;0x01 0x05 0x09 13年5月21⽇日星期⼆二
  • 8. Lets go from C int a;a 0x01 0x05 0x09 13年5月21⽇日星期⼆二
  • 9. Lets go from C int a;a a=5; 0x01 0x05 0x09 13年5月21⽇日星期⼆二
  • 10. Lets go from C int a;a a=5; 50x01 0x05 0x09 13年5月21⽇日星期⼆二
  • 11. Pointer int a; a=5; a 50x01 0x09 0x05 & => address of 13年5月21⽇日星期⼆二
  • 12. Pointer int a; a=5; int * pa; a 50x01 0x09 0x05 & => address of 13年5月21⽇日星期⼆二
  • 13. Pointer int a; a=5; int * pa; pa a 50x01 0x09 0x05 & => address of 13年5月21⽇日星期⼆二
  • 14. Pointer int a; a=5; int * pa; pa a 50x01 0x09 0x05 pa = &a; & => address of 13年5月21⽇日星期⼆二
  • 15. Pointer int a; a=5; int * pa; pa a 50x01 0x09 0x05 pa = &a; 0x01 & => address of 13年5月21⽇日星期⼆二
  • 16. Pointer - More int a; a=5; int * pa; pa a 50x01 0x09 0x05 pa = &a; 0x01 printf(“%d”,*pa); 13年5月21⽇日星期⼆二
  • 17. Recap - pointer Write some thing 13年5月21⽇日星期⼆二
  • 18. Pointer - More int a; a=5; int * pa; pa a 50x01 0x09 0x05 pa = &a; 0x01 13年5月21⽇日星期⼆二
  • 19. Pointer - More int a; a=5; int * pa; pa a 50x01 0x09 0x05 pa = &a; 0x01 *pa = 10 13年5月21⽇日星期⼆二
  • 20. Pointer - More int a; a=5; int * pa; pa a 50x01 0x09 0x05 pa = &a; 0x01 *pa = 10 printf(“%d”,a); 13年5月21⽇日星期⼆二
  • 21. 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⽇日星期⼆二
  • 22. 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⽇日星期⼆二
  • 23. Struct - Memory 0x10date struct Date { int day; int month; int year; }; 13年5月21⽇日星期⼆二
  • 24. Struct - Memory 0x10date struct Date date ; struct Date { int day; int month; int year; }; 13年5月21⽇日星期⼆二
  • 25. Struct - Memory 0x10date struct Date date ; struct Date { int day; int month; int year; }; 13年5月21⽇日星期⼆二
  • 26. Struct - Memory 0x10date struct Date date ; date = {3,10,1970}; struct Date { int day; int month; int year; }; 13年5月21⽇日星期⼆二
  • 27. Struct - Memory 0x10date struct Date date ; date = {3,10,1970}; struct Date { int day; int month; int year; }; 3 10 1970 13年5月21⽇日星期⼆二
  • 28. Function • we know main function • make another function 13年5月21⽇日星期⼆二
  • 29. Function - Memory int add(int a, int b){ int result = a+b; return result; } add result in Stack 13年5月21⽇日星期⼆二
  • 30. • Text Segment - 唯讀執⾏行區 • Data Segment - 可讀寫區包含 global 變數 • Heap - 可依程式需要產⽣生和消除記憶體(動態配置- malloc) • Stack - 為了 function 產⽣生,可變動⼤大⼩小,包含區域變數 Objective-C 程式記憶體配置 Text Segment Data Segment Heap Stack 未使⽤用 記憶體位置增加 Stack Pointer 13年5月21⽇日星期⼆二
  • 31. Stack 與 Function Text Segment Data Segment Heap Stack Stack Pointer int main (int argc, const char * argv[]) { // ... 省略 ! } 13年5月21⽇日星期⼆二
  • 32. 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⽇日星期⼆二
  • 33. 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⽇日星期⼆二
  • 34. 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⽇日星期⼆二
  • 35. 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⽇日星期⼆二
  • 36. 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⽇日星期⼆二
  • 37. 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⽇日星期⼆二
  • 38. Stack 與 Heap Text Segment Data Segment Heap Stack local variable malloc 13年5月21⽇日星期⼆二
  • 39. Pointer - 動態配置記憶體 int * p_a; p_a = malloc(sizeof(int)); *p_a = 8; printf("value in pointer a is %dn", *p_a); #include <stdlib.h> p_a Heap 8 Stack 0xA0 0xA0 13年5月21⽇日星期⼆二
  • 40. 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⽇日星期⼆二
  • 41. 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⽇日星期⼆二
  • 42. Objective-C class - C struct • /usr/include/objc/objc.h typedef struct objc_class *Class; typedef struct objc_object { Class isa; } *id; typedef struct objc_selector !*SEL; typedef id (*IMP)(id, SEL, ...); 13年5月21⽇日星期⼆二
  • 43. SEL struct objc_selector { void *sel_id; const char *sel_types; }; 13年5月21⽇日星期⼆二
  • 44. Objective-C class - C struct • /usr/include/objc/runtime.h struct 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⽇日星期⼆二
  • 45. Method struct 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⽇日星期⼆二
  • 46. About Message • Add method runtime 13年5月21⽇日星期⼆二
  • 47. Add method runtime struct 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⽇日星期⼆二
  • 48. 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 method id 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⽇日星期⼆二
  • 49. 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⽇日星期⼆二
  • 50. Demo • DemoNCCU13 - MyCObject 13年5月21⽇日星期⼆二
  • 51. Dynamic feature • Dot operation • KVC 13年5月21⽇日星期⼆二
  • 52. 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⽇日星期⼆二
  • 53. 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⽇日星期⼆二
  • 55. 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⽇日星期⼆二
  • 56. • Find the action -name or -setName • Find real variable _name and then name Finding rule forKey:@”name” 13年5月21⽇日星期⼆二
  • 57. • attribute • scalar // auto-boxing for KVC • NSString • Boolean • Immutable object : NSColor, NSNumber • to-many relationship - objects in NSArray, NSSet Accessible Types 13年5月21⽇日星期⼆二
  • 58. 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 setter 1. find out _name = @”michael” 2. find out name’s getter, return name @implementation MyObject @synthesize name, _name; @end 下底線開頭的變數要⼩小⼼心使⽤用 13年5月21⽇日星期⼆二
  • 59. [myCar valueForKeyPath: @"engine.vendor"] KeyPath @interface MyCar : NSObject { ! Engine * engine; } @end @interface Engine : NSObject { ! NSString * vendor; } @end 13年5月21⽇日星期⼆二
  • 60. NSNumber * count; count = [myCars valueForKeyPath: @"@avg.price"] Array Operation @interface MyCar : NSObject { ! int price; } @end @sum.xxx @min.xxx @max.xxx myCar1 myCar2 myCar3myCars = 13年5月21⽇日星期⼆二
  • 61. Recap // 計算 engines array裡平均的price NSArray * engines = [???:myengine1, myengine2, myengine3, nil]; NSLog(@" average price is %@",[engines valueForKeyPath:@"???"]); @interface Engine : NSObject { ! int price; } @end 13年5月21⽇日星期⼆二
  • 62. 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⽇日星期⼆二
  • 63. Handle UndefinedKey - (void) setValue: (id) value forUndefinedKey: (NSString *) key { // do something } - (id) valueForUndefinedKey:(NSString *)key { // do something } 13年5月21⽇日星期⼆二
  • 64. Recap 實作 - (void) setValue: (id) value forUndefinedKey: (NSString *) key; - (id) valueForUndefinedKey:(NSString *)key; 13年5月21⽇日星期⼆二
  • 65. Reference Introduction to Key-Value Coding Programming Guide http://goo.gl/rrfmy 13年5月21⽇日星期⼆二
  • 66. 實例 - 解析 RSS 網路上免費資源 - 多⽤用來呈現新聞 13年5月21⽇日星期⼆二
  • 67. 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⽇日星期⼆二
  • 68. 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⽇日星期⼆二