初探swift語言的學習筆記十一(performSelector)

來源:互聯網
上載者:User

標籤:swift   performselector   線程   多線程   extension   

fengsh998原文地址:http://blog.csdn.net/fengsh998/article/details/35842441轉載請註明出處如果覺得文章對你有所協助,請通過留言或關注公眾帳號fengsh998來支援我,謝謝!


在OC中使用好好的performSelector,但不知為什麼在swift有意的被拿掉了。更有甚者連IMP, objc_msgSend也不能用了。雖然想不通為什麼,但應該有他的道理。就不糾結了。

大家可能在OC中使用得更多的就是延時處理,及幕後處理,或線程非同步等。現在沒有performSelector,哪在swift還有什麼可以代替呢,當然有了,如GCD,NSTimer其實都可以近似的代替。

如:NSTimer 

let tm = NSTimer(timeInterval: 3.0, target: self, selector: "selectorfunc:", userInfo: nil, repeats: false)        tm.fire()

但個人感覺使NSTimer有個傳上下文時擷取與performSelector不一樣。從上下文中拿到的是NSTimer需要再取userInfo才可以。但不管怎麼樣,可以實現延時處理。


另外,我還自己做了一個swift 的擴充類(OC的catagray) 同樣偽裝了一個performselector方法。為什麼講是偽裝,因為過程中在調用最終的selector時,用了線程處理。而不是理想的msgSend的方式(swift沒有開源,沒法看到實現,又不提供IMP,查了好多資料都沒有講執行Selector 的,如有大神發現,請告訴我。。。)

實現:

////  ExpandNSObject.swift//  MixDemo////  Created by apple on 14-6-29.//  Copyright (c) 2014年 fengsh. All rights reserved.//import Foundationextension 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操作,需要回到主線程。就這點沒有偽裝好,真失敗。。。。

大家在使用過種中請謹慎,別外,如果有發現BUG,請告訴我。。。。。謝謝。

別附swift源碼檔案位置:
http://download.csdn.net/detail/fengsh998/7569569

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.