Objective-C 2.0 with Cocoa Foundation---NSObject的奥秘(2)

Objective-C 2.0 with Cocoa Foundation---NSObject的奥秘(2),第1张

概述  图6-1,本章程序运行结果   大家看到本章程序的运行结果的屏幕拷贝的时候,也许会觉得很无趣,因为单单从结果画面,我们没有发现任何令人感到很有兴趣的东西,相反,都是同学们已经很熟悉的一些老面孔。但是本章所要讲述的东西也许是同学们在其他语言里面从来没有遇到过的东西,这些东西将会令人感到新鲜和激动。   6.2,实现步骤   第一步,按照我们在第2章所述的方法,新建一个项目,项目的名字叫做06-N

  图6-1,本章程序运行结果

  大家看到本章程序的运行结果的屏幕拷贝的时候,也许会觉得很无趣,因为单单从结果画面,我们没有发现任何令人感到很有兴趣的东西,相反,都是同学们已经很熟悉的一些老面孔。但是本章所要讲述的东西也许是同学们在其他语言里面从来没有遇到过的东西,这些东西将会令人感到新鲜和激动。

  6.2,实现步骤

  第一步,按照我们在第2章所述的方法,新建一个项目,项目的名字叫做06-NSobjecs。

  第二步,按照我们在第4章的4.2节的第二,三,四步所述的方法,把在第4章已经使用过的“cattle.h”,“cattle.m”,“Bull.h”还有“Bull.m” 导入本章的项目里面。

  第三步,把鼠标移动到项目浏览器上面的“Source”上面,然后在d出的菜单上面选择“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所示

总结

以上是内存溢出为你收集整理的Objective-C 2.0 with Cocoa Foundation---NSObject的奥秘(2)全部内容,希望文章能够帮你解决Objective-C 2.0 with Cocoa Foundation---NSObject的奥秘(2)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1065744.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存