Objective C的SEL类型

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        Person* p = [[Person alloc] init];
        // 消息机制
        [p eat];
        
        // 使用@selector就能够把一个方法包装成sel数据类型
        SEL s1 = @selector(eat);
        [p performSelector:s1];
        [p performSelector:@selector(eat)]; // 等同于上面两句话
        
        SEL s2 = @selector(call:);
        [p performSelector:s2 withObject:@"123456"];
        
        SEL s3 = @selector(findName);
        NSString* str = [p performSelector:s3];
        NSLog(@"%@", str);
        
        NSLog(@"Hello, World!");
    }
    return 0;
}

#import <Foundation/Foundation.h>

@interface Person : NSObject
- (void) eat;
-(void) call:(NSString*)number;
-(NSString*)findName;

@end

#import "Person.h"

@implementation Person
- (void) eat{
    NSLog(@"eat food");
}
-(void) call:(NSString*)number {
    NSLog(@"call number:%@", number);
}
-(NSString*)findName {
return @"xman";
}

@end


Objective C的SEL类型_第1张图片


//-------------------------自己------------------------

总结:(1)SEL类型调用的两种形式;

    (2)SEL类型调用,无参、有参数、有返回的形式;

    (3)Objective c工程创建OSX - application---command line tool;

    (4)Objective c工程运行,选择相应OC工程,MyMac上即可;

你可能感兴趣的:(Objective C的SEL类型)