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, const char * 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];
    return 0;
    }

  • 相关阅读:
    HRBUST--2317 Game(完全背包)
    k8s的回滚应用
    python练习-2
    k8s HA 补充-(keepalived+haproxy配置)
    Etcd故障恢复记录
    kubernetes 1.14安装部署helm插件
    k8s Prometheus+CAdvisor+node_export+grafana
    k8s ingress部署
    k8s pvc
    k8s pv
  • 原文地址:https://www.cnblogs.com/chenjunbiao/p/2023196.html
Copyright © 2011-2022 走看看