performSelector & @selector

Object-C中@selector关键字 是用来搜索方法的,将方法转换成SEL类型的变量。

#import <Cocoa/Cocoa.h>

@interface Car : NSObject {
    int year;
    NSString *make;
    NSString *model;

}

@property (nonatomic,retain) NSString * model;
@property (nonatomic ,retain )NSString * make;
-(void) setMake :(NSString *)aMake  andModel:(NSString *) aModel;

@end


上面的代码声明了一个Car类,并且设置其中的两个属性为非原子性的、自动生成getter方法(使用了@property)。

#import "Car.h"

@implementation Car:NSObject
@synthesize make;
@synthesize model;

-(void) setMake:(NSString *)aMake andModel:(NSString *)aModel{
    make=aMake;
    model=aModel;
}

@end

上面代码是实现了Car类  ,使用了@synthesize关键字声明 该属性自动生成setter方法。

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

int main (int argc, const char * argv[]) {
        Car * myCar2=[[Car alloc]init];
    SEL carMethod=@selector(setMake:andModel:);
    [myCar2 performSelector:carMethod withObject:@"11111" withObject:@"2222"   afterDelay:10];
    NSLog(@"myCar shuxing : %@  ---%@",myCar2.make,myCar2.model);
}

这段代码中@selector 搜索setMake方法 并返回SEL类型的变量。

格式如下:

SEL 变量名 =  @selector(方法名 :   第二个参数名 :  参数);

    [myCar2 performSelector:carMethod withObject:@"11111" withObject:@"2222" afterDelay:10];

这句话将搜索到的SEL类型的消息 发送给 myCar2对象。 performSelector关键字可以调用到SEL类型的变量。延迟十秒执行。

其语句格式如下:

[消息接受者     performSelector:SEL类型的变量    withObject:  第一个参数   withObject:  第二个参数   afterDelay :秒数];

- (void)close { // Empty queues. [self emptyQueues]; [partialReadBuffer release]; partialReadBuffer = nil; [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(disconnect) object:nil]; // Close streams. if (theReadStream != NULL) { CFReadStreamSetClient(theReadStream, kCFStreamEventNone, NULL, NULL); CFReadStreamUnscheduleFromRunLoop (theReadStream, theRunLoop, kCFRunLoopDefaultMode); CFReadStreamClose (theReadStream); CFRelease (theReadStream); theReadStream = NULL; } if (theWriteStream != NULL) { CFWriteStreamSetClient(theWriteStream, kCFStreamEventNone, NULL, NULL); CFWriteStreamUnscheduleFromRunLoop (theWriteStream, theRunLoop, kCFRunLoopDefaultMode); CFWriteStreamClose (theWriteStream); CFRelease (theWriteStream); theWriteStream = NULL; } // Close sockets. if (theSocket != NULL) { CFSocketInvalidate (theSocket); CFRelease (theSocket); theSocket = NULL; } if (theSocket6 != NULL) { CFSocketInvalidate (theSocket6); CFRelease (theSocket6); theSocket6 = NULL; } if (theSource != NULL) { CFRunLoopRemoveSource (theRunLoop, theSource, kCFRunLoopDefaultMode); CFRelease (theSource); theSource = NULL; } if (theSource6 != NULL) { CFRunLoopRemoveSource (theRunLoop, theSource6, kCFRunLoopDefaultMode); CFRelease (theSource6); theSource6 = NULL; } theRunLoop = NULL; // If the client has passed the connect/accept method, then the connection has at least begun. // Notify delegate that it is now ending. if (theFlags &amp; kDidPassConnectMethod) { // Delay notification to give him freedom to release without returning here and core-dumping. if ([theDelegate respondsToSelector: @selector(onSocketDidDisconnect:)]) { //[theDelegate performSelector:@selector(onSocketDidDisconnect:) withObject:self afterDelay:0]; [theDelegate onSocketDidDisconnect:self]; } } // Clear flags. theFlags = 0x00; }
最新发布
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值