美文网首页
Objective-C语法之动态类型(isKindOfClass

Objective-C语法之动态类型(isKindOfClass

作者: 恋空K | 来源:发表于2018-12-05 14:12 被阅读10次

    判断对象类型

    -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例

    -(BOOL) isMemberOfClass: classObj 判断是否是这个类的实例

    我们试试这两个方法的使用。

    1、新建Person类继承NSObject,新建Teacher类继承Person

    1.1、新建Person类

    #import<Foundation/Foundation.h>

    @interface Person : NSObject

    {

        NSString *name;

    }

    -(void)setName:(NSString*)n;

    @end

    #import"Person.h"

    @implementation Person

    -(void)setName:(NSString *)n

    {

        name = n;

    }

    @end

    1.2新建Teacher类

    #import"Person.h"

    @interface Teacher : Person

    -(void)teach;

    @end

    #import"Teacher.h"

    @implementation Teacher

    -(void)teach

    {

    NSLog(@"我教数学");

    }

    @end

    1.3 我们先实验下isMemberOfClass方法。

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        Person *person = [[Person alloc] init];

        Teacher *teacher = [[Teacher alloc] init];

        //YES

        if ([teacher isMemberOfClass:[Teacher class]]) {

            NSLog(@"teacher Teacher类的成员");

        }

        //NO

        if ([teacher isMemberOfClass:[Person class]]) {

            NSLog(@"teacher Person类的成员");

        }

        //NO

        if ([teacher isMemberOfClass:[NSObject class]]) {

            NSLog(@"teacher NSObject类的成员");

        }

        [person release];

        [teacher release];

        [pool release];

    打印结果:

    2012-07-04 14:23:07.965 ObjectiveCTest[2460:f803] teacher Teacher类的成员

    只有第一个判断打印出来,isMemberOfClass判断是否是属于这类的实例,是否跟父类有关系他不管。

    1.4isKindOfClass方法

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        Person *person = [[Person alloc] init];

        Teacher *teacher = [[Teacher alloc] init];

        //YES

        if ([teacher isKindOfClass:[Teacher class]]) {

            NSLog(@"teacher 是 Teacher类或Teacher的子类");

        }

        //YES

        if ([teacher isKindOfClass:[Person class]]) {

            NSLog(@"teacher 是 Person类或Person的子类");

        }

        //YES

        if ([teacher isKindOfClass:[NSObject class]]) {

            NSLog(@"teacher 是 NSObject类或NSObject的子类");

        }

        [person release];

        [teacher release];

        [pool release];

    2012-07-04 14:34:17.315 ObjectiveCTest[2595:f803] teacher 是 Teacher类或Teacher的子类

    2012-07-04 14:34:17.316 ObjectiveCTest[2595:f803] teacher 是 Person类或Person的子类

    2012-07-04 14:34:17.316 ObjectiveCTest[2595:f803] teacher 是 NSObject类或NSObject的子类

    三个结果都打印出来了。

    2、

    -(BOOL) respondsToSelector: selector 判读实例是否有这样方法

    +(BOOL) instancesRespondToSelector:  判断类是否有这个方法。此方法是类方法,不能用在类的对象

    2.1 respondsToSelector的使用

    这里不写对象的创建和释放了,参考上面的代码

    // YES

        if ( [teacher respondsToSelector: @selector( setName: )] == YES ) {

            NSLog(@"teacher responds to setSize: method" );

        }

        // NO

        if ( [teacher respondsToSelector: @selector( abcde )] == YES ) {

            NSLog(@"teacher responds to nonExistant method" );

        }

        // YES

        if ( [teacher respondsToSelector: @selector( alloc )] == YES ) {

            NSLog(@"teacher class responds to alloc method\n" );

        }

    打印结果:

    2012-07-04 14:39:49.853 ObjectiveCTest[2723:f803] teacher responds to setSize: method

    2012-07-04 14:39:49.854 ObjectiveCTest[2723:f803] teacher class responds to alloc method

    中间的那个判断我随便写了个selector,当然没有了。respondsToSelector 检查类方法 alloc返回YES

    2.2 instancesRespondToSelector

    // NO

        if ( [Person instancesRespondToSelector: @selector(teach)] == YES ) {

            NSLog(@"Person instance responds to teach method" );

        }

        // YES

        if ( [Teacher instancesRespondToSelector: @selector(teach)] == YES ) {

            NSLog(@"Teacher instance responds to teach method");

        }

        // YES

        if ( [Teacher instancesRespondToSelector: @selector(setName:)] == YES ) {

            NSLog(@"Teacher instance responds to setName: method" );

        }

    相关文章

      网友评论

          本文标题:Objective-C语法之动态类型(isKindOfClass

          本文链接:https://www.haomeiwen.com/subject/qpoicqtx.html