图6-1,本章程序运行结果
大家看到本章程序的运行结果的屏幕拷贝的时候,也许会觉得很无趣,因为单单从结果画面,我们没有发现任何令人感到很有兴趣的东西,相反,都是同学们已经很熟悉的一些老面孔。但是本章所要讲述的东西也许是同学们在其他语言里面从来没有遇到过的东西,这些东西将会令人感到新鲜和激动。
6.2,实现步骤
第一步,按照我们在第2章所述的方法,新建一个项目,项目的名字叫做06-NSobjecs。
第二步,按照我们在第4章的4.2节的第二,三,四步所述的方法,把在第4章已经使用过的“Cattle.h”,“Cattle.m”,“Bull.h”还有“Bull.m” 导入本章的项目里面。
第三步,把鼠标移动到项目浏览器上面的“Source”上面,然后在弹出的菜单上面选择“Add”,然后在子菜单里面选择“New File”,然后在新建文件对话框的左侧最下面选择“Other”,然后在右侧窗口选择“Empty File”,选择“Next”,在“New File”对话框里面的“File Name”栏内输入“MyNSobjecs.h”。然后输入(或者是拷贝也可以,因为这是C的代码,如果你很熟悉C语言的话,可以拷贝一下节省时间)如下代码:
#include
typedef const struct objc_selector
{
void *sel_id;
const char *sel_types;
} *MySEL;
typedef struct my_objc_objecs {
struct my_objc_class* class_pointer;
} *myId;
typedef myId (*MyIMP)(myId, MySEL, );
typedef char *STR; /* String alias */
typedef struct my_objc_class *meteClass;
typedef struct my_objc_class *MyClass;
struct my_objc_class {
meteClass class_pointer;
struct my_objc_class* super_class;
const char* name;
long version;
unsigned long info;
long instance_size;
struct objc_ivar_list* ivars;
struct objc_method_list* methods;
struct sarray * dtable;
struct my_objc_class* subclass_list;
struct my_objc_class* sibling_class;
struct objc_protocol_list *protocols;
void* gc_objecs_type;
};
typedef struct objc_protocol {
struct my_objc_class* class_pointer;
char *protocol_name;
struct objc_protocol_list *protocol_list;
struct objc_method_description_list *instance_methods, *class_methods;
} Protocol;
typedef void* retval_t;
typedef void(*apply_t)(void);
typedef union arglist {
char *arg_ptr;
char arg_regs[sizeof (char*)];
} *arglist_t;
typedef struct objc_ivar* Ivar_t;
typedef struct objc_ivar_list {
int ivar_count;
struct objc_ivar {
const char* ivar_name;
const char* ivar_type;
int ivar_offset;
} ivar_list[1];
} IvarList, *IvarList_t;
typedef struct objc_method {
MySEL method_name;
const char* method_types;
MyIMP method_imp;
} Method, *Method_t;
typedef struct objc_method_list {
struct objc_method_list* method_next;
int method_count;
Method method_list[1];
} MethodList, *MethodList_t;
struct objc_protocol_list {
struct objc_protocol_list *next;
size_t count;
Protocol *list[1];
};
第四步,打开06-NSobjecs.m文件,输入如下代码并且保存
#import
#import "Cattle.h"
#import "Bull.h"
#import "MyNSobjecs.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
id cattle = [Cattle new];
id redBull = [Bull new];
SEL setLegsCount_SEL = @selector(setLegsCount:);
IMP cattle_setLegsCount_IMP = [cattle methodForSelector:setLegsCount_SEL];
IMP redBull_setLegsCount_IMP = [redBull methodForSelector:setLegsCount_SEL];
[cattle setLegsCount:4];
[redBull setLegsCount:4];
[redBull setSkinColor:@"red"];
Class cattle_class = cattle->isa;
MyClass my_cattle_class = cattle->isa;
SEL say = @selector(saySomething);
IMP cattle_sayFunc = [cattle methodForSelector:say];
cattle_sayFunc(cattle, say);
Class redBull_class = redBull->isa;
MyClass my_redBull_class = redBull->isa;
IMP redBull_sayFunc = [redBull methodForSelector:say];
redBull_sayFunc(redBull, say);
[pool drain];
return 0;
}
第五步,在06-NSobjecs.m文件的窗口的“[pool drain];”代码的左侧单击一下窗口的边框,确认一下是否出现一个蓝色的小棒棒,如果有的话那么断点被选择好了。如图6-2所示