NSObject.h里的方法.

#import <Foundation/Foundation.h> #import "Caller.h" int main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; /*分析NSObject中的方法*/ Caller *c = [[Caller alloc] init]; NSLog(@"c retain count: %d", [c retainCount]); Caller *c1 = [[Caller alloc] init]; //1.- (Class) class; /** See [NSObject-class] */ NSLog(@"class: %@", [c class]); //2.- (Class) superclass; /** See [NSObject-superclass] */ //父类是 NSObject NSLog(@"super class: %@", [c superclass]); //3.- (BOOL) isEqual: (id)anObject; /** See [NSObject-isEqual:] */ //从下面的例子可以看出isEqual是比较的地址指针,[c retain]出来的指针是一样的所以相等 c1的指针和c不一样 所以不相等 if ([c isEqual:[c retain]]){ NSLog(@"isEqual [c retain]"); } NSLog(@"c retain count: %d", [c retainCount]); if (![c isEqual:c1]){ NSLog(@"is not Equal c1"); } //4. - (BOOL) isKindOfClass: (Class)aClass; /** See [NSObject-isKindOfClass:] */ if ([c isKindOfClass:[NSObject class]]){ NSLog(@"isKindOfClass NSObject"); } //5. - (NSUInteger) hash; /** See [NSObject-hash] */ NSLog(@"%d", [c hash]); //6. - (id) self; /** See [NSObject-self] */ //从下面的例子可以看出 self 方法不会增加对象的retainCount, 只是在上面的retain的时候才增加了一个, self方法取得的就是对象本身 Caller *c2 = [c self]; NSLog(@"c retain count: %d", [c retainCount]); //7.- (BOOL) respondsToSelector: (SEL)aSelector; //从下面例子可以看出这个方法就是判断某个对象是不是有某个方法 if ([c respondsToSelector:@selector(init)]){ NSLog(@"the c object have init function."); } //8.- (id) retain //引用计数+1 Caller *c3 = [c retain]; NSLog(@"c retain count: %d", [c retainCount]); //9.- (id) autorelease /** See [NSObject-autorelease] */; //从下面的例子可以看出调用autorelease 引用计数不增加 //Caller *c4 = [c autorelease]; //NSLog(@"c retain count: %d", [c4 retainCount]); //10.+ (id) alloc //调用alloc 会生成新的对象并且重新分配内存,引用计数会+1 Caller *c5 = [Caller alloc]; NSLog(@"c5 retain count: %d", [c5 retainCount]); NSLog(@"%@", [c5 description]); //11. -(id)init //调用init方法不会生成新的对象,只是返回当前对象本身,引用计数也不会增加. [c5 retain]; Caller *c6 = [c5 init]; NSLog(@"%@", [c5 description]); NSLog(@"c6 retain count: %d", [c6 retainCount]); //12.+ (id) new; //调用new方法会生成新的对象,并且重新分配内存,并且自动执行对象的init方法. //所以new = 先执行对象的alloc方法,然后在执行init方法. Caller *c7 = [Caller new]; [c7 retain]; NSLog(@"%@", [c7 description]); NSLog(@"c7 retain count: %d", [c7 retainCount]); //13. - (id) copy; //如果对象没有实现NSCopying协议而直接copy对象则会出错. //从下面例子可以看出执行copy方法会生成一个新的对象 并且重新分配内存.实际上就是执行NSCopying协议的copyWithZone方法的结果 Caller *c8 = [c7 copy]; NSLog(@"%@", [c8 description]); NSLog(@"c8 retain count: %d", [c8 retainCount]); // [c8 release]; //14. - (BOOL) conformsToProtocol: (Protocol*)aProtocol //判断一个class是否实现了某个协议 if ([c8 conformsToProtocol:@protocol(NSCopying)]){ NSLog(@"The Class is conform to NSCopying protocol."); } if ([c8 conformsToProtocol:@protocol(NSCoding)]){ NSLog(@"The Class is conform to NSCopying protocol."); }else{ NSLog(@"The Class is not conform to NSCopying protocol."); } //15.- (NSZone*) zone; /** See [NSObject-zone] */ //返回一个class的zone, zone是一个结构 NSZone *zone = [c8 zone]; NSLog(@"zone : %@", zone->name); //NSZone *nZone = zone->next; //NSLog(@"next zone: %@", nZone->name); //16. - (NSString*) className; //返回class name NSLog(@"class name : %@", [c8 className]); //17. + (id) allocWithZone: (NSZone*)z; Caller *c9 = [Caller allocWithZone:[c8 zone]]; NSLog(@"c9 desc : %@", [c9 description]); NSLog(@"c9 retain count: %d", [c9 retainCount]); //18. + (IMP) instanceMethodForSelector: (SEL)aSelector; //此方法返回aSelector的函数指针 IMP imp = [Caller instanceMethodForSelector:@selector(start3)]; id i = imp([Caller new], @selector(start3));//调用函数指针,需要带入参数 (类的实例 方法的名称) //19. + (NSMethodSignature*) instanceMethodSignatureForSelector: (SEL)aSelector; //此方法返回一个NSMethodSignature(方法签名?)对象.NSMethodSignature可以用于反射调用 NSMethodSignature *sig = [Caller instanceMethodSignatureForSelector:@selector(start3)]; NSLog(@"numberOfArguments %d", [sig numberOfArguments]); NSLog(@"methodReturnLength %d", [sig methodReturnLength]); //反射调用 NSInvocation *ivc = [NSInvocation invocationWithMethodSignature:sig]; [ivc setTarget:[Caller new]]; [ivc setSelector:@selector(start3)]; [ivc invoke]; //20. + (BOOL) instancesRespondToSelector: (SEL)aSelector; if ([Caller instancesRespondToSelector:@selector(start3)]){ NSLog(@"Caller instancesRespondToSelector start3"); } if ([Caller instancesRespondToSelector:@selector(start7)]){ NSLog(@"Caller instancesRespondToSelector start7"); }else{ NSLog(@"Caller not instancesRespondToSelector start7"); } //21. + (BOOL) isSubclassOfClass: (Class)aClass; //判断是不是子类 if ([Caller isSubclassOfClass:[NSObject class]]){ NSLog(@"Caller isSubclassOfClass of NSObject"); } //22. +(void) poseAsClass: (Class)aClassObject; [Caller poseAsClass:[NSObject class]]; [c1 release]; [c release]; [pool drain]; return 0; }

你可能感兴趣的:(c,object,Class,import)