SlideShare a Scribd company logo
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

PDF
C++11 smart pointers
PDF
OOP in C - Virtual Function (Chinese Version)
PPTX
xwz 2010-10-31
PDF
Gobject - Inherit (Chinese)
PPT
Introduction to C++ over CLI
PPTX
認識 C++11 新標準及使用 AMP 函式庫作平行運算
PPTX
Dev307
PDF
twMVC#27 | C# 7.0 新功能介紹
C++11 smart pointers
OOP in C - Virtual Function (Chinese Version)
xwz 2010-10-31
Gobject - Inherit (Chinese)
Introduction to C++ over CLI
認識 C++11 新標準及使用 AMP 函式庫作平行運算
Dev307
twMVC#27 | C# 7.0 新功能介紹

Viewers also liked (17)

PDF
Autorelease pool
PDF
Core data lightweight_migration
PDF
Homework2 play cards
PPTX
比價撿便宜 Steven
PDF
Dropbox sync
PDF
Note something
PDF
Google maps SDK for iOS 1.4
PDF
Activity
PDF
Prototype by Xcode
PDF
Shootting Game
PDF
Strategy Pattern for Objective-C
PDF
Eclipse and Genymotion
PPSX
C programming structure & pointer
PPT
C Structures & Unions
PDF
Introduction to Android Studio
PPTX
Data types
PPT
Programming in c
Autorelease pool
Core data lightweight_migration
Homework2 play cards
比價撿便宜 Steven
Dropbox sync
Note something
Google maps SDK for iOS 1.4
Activity
Prototype by Xcode
Shootting Game
Strategy Pattern for Objective-C
Eclipse and Genymotion
C programming structure & pointer
C Structures & Unions
Introduction to Android Studio
Data types
Programming in c
Ad

Similar to Objc under the_hood_2013 (20)

PDF
02 Objective-C
PDF
iOs app 101
PDF
Programming in Objective-C
PPT
ios分享
DOC
Lesson 2 Basicstructure
ODP
Object-Based Programming Part II
PPT
iPhone,ios,Object-C基础入门
PPT
iPhone,ios,Object-c基础入门
PDF
SurveyiOSCodingStyleFromAppleDocument
PDF
105-2 iOS程式設計(二)
PPTX
Chapter 6 point (c)
PDF
iOS 入門教學
PPTX
從 Singleton 談 constructor
PPTX
IOS入门分享
PDF
Mokoversity Course: Apple Swift 101 - Introduction
PDF
看似比較簡單的推坑教學 C語言從崩潰到崩潰Ex(二)
PDF
PDF
PDF
PDF
02 Objective-C
iOs app 101
Programming in Objective-C
ios分享
Lesson 2 Basicstructure
Object-Based Programming Part II
iPhone,ios,Object-C基础入门
iPhone,ios,Object-c基础入门
SurveyiOSCodingStyleFromAppleDocument
105-2 iOS程式設計(二)
Chapter 6 point (c)
iOS 入門教學
從 Singleton 談 constructor
IOS入门分享
Mokoversity Course: Apple Swift 101 - Introduction
看似比較簡單的推坑教學 C語言從崩潰到崩潰Ex(二)
Ad

More from Michael Pan (7)

PDF
Nimbus
PDF
Superstar dj pdf
KEY
ADB - Arthur
KEY
Appsgaga - iOS Game Developer
PPT
GZFox Inc. Jacky
PPT
創投公司 hoku_20121017
KEY
Opening iOS App 開發者交流會
Nimbus
Superstar dj pdf
ADB - Arthur
Appsgaga - iOS Game Developer
GZFox Inc. Jacky
創投公司 hoku_20121017
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⽇日星期⼆二