初探swift语言的学习笔记十一(performSelector)
生活随笔
收集整理的這篇文章主要介紹了
初探swift语言的学习笔记十一(performSelector)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作者:fengsh998
原文地址:http://blog.csdn.net/fengsh998/article/details/35842441
轉載請注明出處
如果覺得文章對你有所幫助,請通過留言或關注微信公眾帳號fengsh998來支持我,謝謝!
let?tm?=?NSTimer(timeInterval:?3.0,?target:?self,?selector:?"selectorfunc:",?userInfo:?nil,?repeats:?false)?? ????????tm.fire()??
但個人感覺使NSTimer有個傳上下文時獲取與performSelector不一樣。從上下文中拿到的是NSTimer需要再取userInfo才可以。但不管怎么樣,可以實現延時處理。 //?? //??ExpandNSObject.swift?? //??MixDemo?? //?? //??Created?by?apple?on?14-6-29.?? //??Copyright?(c)?2014年?fengsh.?All?rights?reserved.?? //?? ?? import?Foundation?? ?? extension?NSObject?? {?? ????func?performSelectorOnMainThread(selector?aSelector:?Selector,withObject?object:AnyObject!?,waitUntilDone?wait:Bool)?? ????{?? ????????if?self.respondsToSelector(aSelector)?? ????????{?? ????????????var?continuego?=?false?? ????????????let?group?=?dispatch_group_create()?? ????????????let?queue?=?dispatch_queue_create("com.fsh.dispatch",?nil)?? ????????????dispatch_group_async(group,queue,{?? ????????????????dispatch_async(queue?,{?? ????????????????????//做了個假的?? ????????????????????NSThread.detachNewThreadSelector(aSelector,?toTarget:self,?withObject:?object)?? ????????????????????continuego?=?true?? ????????????????})?? ????????????????})?? ????????????dispatch_group_wait(group,?DISPATCH_TIME_FOREVER)?? ?? ????????????if?wait?? ????????????{?? ????????????????let?ret?=?NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode,?beforeDate:?NSDate.distantFuture()?as?NSDate)?? ????????????????while?(!continuego?&&?ret)?? ????????????????{?? ?????????????????????? ????????????????}?? ????????????}?? ????????}?? ????}?? ?????? ????func?performSelector(selector?aSelector:?Selector,?object?anArgument:?AnyObject!?,delay?afterDelay:NSTimeInterval)?? ????{?? ????????if?self.respondsToSelector(aSelector)?? ????????{?? ????????????let?minseconds?=?afterDelay?*?Double(NSEC_PER_SEC)?? ????????????let?dtime?=?dispatch_time(DISPATCH_TIME_NOW,?Int64(minseconds))?? ?? ????????????dispatch_after(dtime,dispatch_get_current_queue()?,?{?? ????????????????//做了個假的?? ????????????????NSThread.detachNewThreadSelector(aSelector,?toTarget:self,?withObject:?anArgument)?? ????????????????})?? ????????}?? ????}?? }??
測試使用: self.performSelector(selector:"refreshTable:",?object:nil?,delay:3.0)?? self.performSelectorOnMainThread(selector:"refreshTable:",?withObject:nil,waitUntilDone:true)??
警告,如果使用performSelector最終執行的selector是在子線程中,如果seleoctor中有更新UI操作,需要回到主線程。就這點沒有偽裝好,真失敗。。。。
在OC中使用好好的performSelector,但不知為什么在swift有意的被拿掉了。更有甚者連IMP, objc_msgSend也不能用了。雖然想不通為什么,但應該有他的道理。就不糾結了。
大家可能在OC中使用得更多的就是延時處理,及后臺處理,或線程異步等。現在沒有performSelector,哪在swift還有什么可以代替呢,當然有了,如GCD,NSTimer其實都可以近似的代替。
如:NSTimer?
[cpp]?view plaincopy
但個人感覺使NSTimer有個傳上下文時獲取與performSelector不一樣。從上下文中拿到的是NSTimer需要再取userInfo才可以。但不管怎么樣,可以實現延時處理。
另外,我還自己做了一個swift 的擴展類(OC的catagray) 同樣偽裝了一個performselector方法。為什么講是偽裝,因為過程中在調用最終的selector時,用了線程處理。而不是理想的msgSend的方式(swift沒有開源,沒法看到實現,又不提供IMP,查了好多資料都沒有講執行Selector 的,如有大神發現,請告訴我。。。)
實現:
[cpp]?view plaincopy
測試使用:
[cpp]?view plaincopy
大家在使用過種中請謹慎,別外,如果有發現BUG,請告訴我。。。。。謝謝。
別附swift源碼文件位置:
http://download.csdn.net/detail/fengsh998/7569569
總結
以上是生活随笔為你收集整理的初探swift语言的学习笔记十一(performSelector)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初探swift语言的学习笔记十(bloc
- 下一篇: IOS7使用原生API进行二维码和条形码