OC SEL


//
// 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

aSelector

A selector identifying the message to send. If aSelector is NULL, anNSInvalidArgumentException is raised.

Returns

An object that is the result of the message.

Availability

OS X (10.0 and later)

Declared In

NSObject.h

Reference

NSObject Protocol Reference



NSString * NSStringFromSelector (

   SEL aSelector

);

Description

Returns a string representation of a given selector.

Parameters

aSelector

A selector.

Returns

A string representation of aSelector.

Availability

OS X (10.0 and later)

Declared In

NSObjCRuntime.h

Reference

Foundation Functions 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

aSelector

A selector identifying the message to send. If aSelector is NULL, anNSInvalidArgumentException is raised.

anObject

An object that is the sole argument of the message.

Returns

An object that is the result of the message.

Availability

OS X (10.0 and later)

Declared In

NSObject.h

Reference

NSObject Protocol 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

aSelectorName

A string of any length, with any characters, that represents the name of a selector.

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

NSObjCRuntime.h

Reference

Foundation Functions Reference


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值