//
// Person.h
// SEL
//
// Created by LiuWei on 15/4/15.
// Copyright (c) 2015年 LiuWei. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
- (void)test;
- (void)test2;
- (void)test3withStr1:(NSString*)str;
@end
//
// Person.m
// SEL
//
// Created by LiuWei on 15/4/15.
// Copyright (c) 2015年 LiuWei. All rights reserved.
//
#import "Person.h"
@implementation Person
- (void)test
{
// 每个对象方法内部都有一个隐藏的 _cmd 代表当前方法
// NSStringFromSelector(SEL) 把选择器转换成字符串
NSString *str = NSStringFromSelector(_cmd);
NSLog(@"_cmd --- %@", str);
NSLog(@"Person --- test");
}
- (void)test2
{
NSLog(@"Person --- test2");
}
- (void)test3withStr1:(NSString *)str
{
NSLog(@"%@", str);
}
@end
//
// main.m
// SEL
//
// Created by LiuWei on 15/4/15.
// Copyright (c) 2015年 LiuWei. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
// 每个类的方法列表都存储在类对象中
// 第个方法都有一个与之对应的SEL数据
int main()
{
Person *p = [[Person alloc]init];
// 调用test方法时会把方法名 test包装成 SEL类型数据
// 根据SEL类型数据找到方法地址
// 根据方法地址,调用相应方法
[p test];
/// An opaque type that represents a method selector.
// typedef struct objc_selector *SEL;
[p performSelector:@selector(test2)];
[p performSelector:@selector(test3withStr1:) withObject:@"hahaha!"];
NSString * str = @"test2";
SEL s = NSSelectorFromString(str);
[p performSelector:s];
return 0;
}
- (id)performSelector:(SEL)aSelector | |||
Description | Sends a specified message to the receiver and returns the result of the message. (required) | ||
| The performSelector: method is equivalent to sending anaSelector message directly to the receiver. For example, all three of the following messages do the same thing: id myClone = [anObject copy]; id myClone = [anObject performSelector:@selector(copy)]; id myClone = [anObject performSelector:sel_getUid("copy")]; However, the performSelector: method allows you to send messages that aren’t determined until runtime. A variable selector can be passed as the argument: SEL myMethod = findTheAppropriateSelectorForTheCurrentSituation(); [anObject performSelector:myMethod]; The aSelector argument should identify a method that takes no arguments. For methods that return anything other than an object, useNSInvocation. | ||
Parameters |
| ||
Returns | An object that is the result of the message. | ||
Availability | OS X (10.0 and later) | ||
Declared In | |||
Reference |
NSString * NSStringFromSelector ( SEL aSelector ); | |||
Description | Returns a string representation of a given selector. | ||
Parameters |
| ||
Returns | A string representation of aSelector. | ||
Availability | OS X (10.0 and later) | ||
Declared In | |||
Reference |
- (id)performSelector:(SEL)aSelector withObject:(id)anObject | |||||
Description | Sends a message to the receiver with an object as the argument. (required) | ||||
| This method is the same as performSelector: except that you can supply an argument foraSelector. aSelector should identify a method that takes a single argument of typeid. For methods with other argument types and return values, useNSInvocation. | ||||
Parameters |
| ||||
Returns | An object that is the result of the message. | ||||
Availability | OS X (10.0 and later) | ||||
Declared In | |||||
Reference |
SEL NSSelectorFromString ( NSString *aSelectorName ); | |||
Description | Returns the selector with a given name. | ||
| To make a selector, NSSelectorFromString passes a UTF-8 encoded character representation ofaSelectorName to sel_registerName and returns the value returned by that function. Note, therefore, that if the selector does not exist it is registered and the newly-registered selector is returned. Recall that a colon (“:”) is part of a method name;setHeight is not the same as setHeight:. | ||
Parameters |
| ||
Returns | The selector named by aSelectorName. IfaSelectorName is nil, or cannot be converted to UTF-8 (this should be only due to insufficient memory), returns(SEL)0. | ||
Availability | OS X (10.0 and later) | ||
Declared In | |||
Reference |