swift语言的学习笔记十一(performSelector)

在OC中使用好好的performSelector,但不知为什么在swift有意的被拿掉了。更有甚者连IMP, objc_msgSend也不能用了。虽然想不通为什么,但应该有他的道理。就不纠结了。

大家可能在OC中使用得更多的就是延时处理,及后台处理,或线程异步等。现在没有performSelector,哪在swift还有什么可以代替呢,当然有了,如GCD,NSTimer其实都可以近似的代替。

如:NSTimer 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. let tm = NSTimer(timeInterval: 3.0, target: self, selector: "selectorfunc:", userInfo: nil, repeats: false)  
  2.         tm.fire()  

但个人感觉使NSTimer有个传上下文时获取与performSelector不一样。从上下文中拿到的是NSTimer需要再取userInfo才可以。但不管怎么样,可以实现延时处理。


另外,我还自己做了一个swift 的扩展类(OC的catagray) 同样伪装了一个performselector方法。为什么讲是伪装,因为过程中在调用最终的selector时,用了线程处理。而不是理想的msgSend的方式(swift没有开源,没法看到实现,又不提供IMP,查了好多资料都没有讲执行Selector 的,如有大神发现,请告诉我。。。)

实现:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  ExpandNSObject.swift  
  3. //  MixDemo  
  4. //  
  5. //  Created by apple on 14-6-29.  
  6. //  Copyright (c) 2014年 fengsh. All rights reserved.  
  7. //  
  8.   
  9. import Foundation  
  10.   
  11. extension NSObject  
  12. {  
  13.     func performSelectorOnMainThread(selector aSelector: Selector,withObject object:AnyObject! ,waitUntilDone wait:Bool)  
  14.     {  
  15.         if self.respondsToSelector(aSelector)  
  16.         {  
  17.             var continuego = false  
  18.             let group = dispatch_group_create()  
  19.             let queue = dispatch_queue_create("com.fsh.dispatch", nil)  
  20.             dispatch_group_async(group,queue,{  
  21.                 dispatch_async(queue ,{  
  22.                     //做了个假的  
  23.                     NSThread.detachNewThreadSelector(aSelector, toTarget:self, withObject: object)  
  24.                     continuego = true  
  25.                 })  
  26.                 })  
  27.             dispatch_group_wait(group, DISPATCH_TIME_FOREVER)  
  28.   
  29.             if wait  
  30.             {  
  31.                 let ret = NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate.distantFuture() as NSDate)  
  32.                 while (!continuego && ret)  
  33.                 {  
  34.                       
  35.                 }  
  36.             }  
  37.         }  
  38.     }  
  39.       
  40.     func performSelector(selector aSelector: Selector, object anArgument: AnyObject! ,delay afterDelay:NSTimeInterval)  
  41.     {  
  42.         if self.respondsToSelector(aSelector)  
  43.         {  
  44.             let minseconds = afterDelay * Double(NSEC_PER_SEC)  
  45.             let dtime = dispatch_time(DISPATCH_TIME_NOW, Int64(minseconds))  
  46.   
  47.             dispatch_after(dtime,dispatch_get_current_queue() , {  
  48.                 //做了个假的  
  49.                 NSThread.detachNewThreadSelector(aSelector, toTarget:self, withObject: anArgument)  
  50.                 })  
  51.         }  
  52.     }  
  53. }  

测试使用:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. self.performSelector(selector:"refreshTable:", object:nil ,delay:3.0)  
  2. self.performSelectorOnMainThread(selector:"refreshTable:", withObject:nil,waitUntilDone:true)  
警告,如果使用performSelector最终执行的selector是在子线程中,如果seleoctor中有更新UI操作,需要回到主线程。就这点没有伪装好,真失败。。。。

大家在使用过种中请谨慎,别外,如果有发现BUG,请告诉我。。。。。谢谢。

别附swift源码文件位置:
http://download.csdn.net/detail/fengsh998/7569569

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
- (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 & 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、付费专栏及课程。

余额充值