zoukankan      html  css  js  c++  java
  • respondsToSelector判断是否实现了某方法

    Tester.h

     

    复制代码
    #import <Foundation/Foundation.h>


    @interface Tester : NSObject {

    }

    -(void) test:(NSString*) msg;

    -(void) notImp;

    @end
    复制代码
    Tester.m
    复制代码
    #import "Tester.h"

    @implementation Tester

    -(void) test:(NSString*) msg
    {
    NSLog(
    @"%@", msg);
    }

    @end
    复制代码
    注意:没有实现notImp方法
     

    main.m

     

    复制代码
    #import <Foundation/Foundation.h>
    #import
    "Tester.h"

    int main (int argc, constchar* argv[])
    {

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

    id tester
    = [[Tester alloc] init];//注意,这里使用id

    SEL testSelector
    = @selector(test:);
    SEL notImpSelector
    = @selector(notImp:);

    if([tester respondsToSelector:testSelector])
    {
    //tester.m中实现了test方法
    [tester test:@"invoke test method"];
    }
    if([tester respondsToSelector:notImpSelector])
    {
    //test.m中没有实现此主就去
    [tester notImp];
    }


    [pool drain];
    return0;
    }
  • 相关阅读:
    2. 开关电源.电感
    1. 开关电源.引子
    资源介绍
    3. EMC EMS EMI
    2. 基于MCU应用的EMC指南
    1. 内部管脚电路
    9.150 Predefined macros
    海康安防平台
    Redis常见配置
    利用python检测单词的相似度
  • 原文地址:https://www.cnblogs.com/zhwl/p/2874700.html
Copyright © 2011-2022 走看看