日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

初探swift语言的学习笔记五(线程)

發布時間:2025/7/14 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 初探swift语言的学习笔记五(线程) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

作者:fengsh998原文地址:http://blog.csdn.net/fengsh998/article/details/30354127轉載請注明出處假設認為文章對你有所幫助,請通過留言或關注微信公眾帳號fengsh998來支持我,謝謝!


swift 并沒有使用新一套線程,使用OC源有的一套線程。以下以樣例來演示一下swift中使用線程。

其用包含常見的:NSThread,NSOperationQueue,GCG

import UIKitclass swiftThreadDemo : UIViewController {var queue = NSOperationQueue() // init() // { // //alloc // super.init() // }deinit{//dealloc}func testGCDThread(){dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {//這里寫須要大量時間的代碼for var i = 0; i < 100000; i++{println("GCD thread running.")}sleep(5);dispatch_async(dispatch_get_main_queue(), {//這里返回主線程,寫須要主線程運行的代碼println("這里返回主線程,寫須要主線程運行的代碼")})})}func testNSThread(){//方式一//NSThread.detachNewThreadSelector("threadInMainMethod:",toTarget:self,withObject:nil)//方式二var myThread = NSThread(target:self,selector:"threadInMainMethod:",object:nil)myThread.start()}func threadInMainMethod(sender : AnyObject){for var i = 0; i < 100000; i++{println("NSThread running.")}sleep(5);println("NSThread over.")}func testNSOperationQueue(){//func (op: NSOperation!)var mopt = myOperationThread()queue.addOperation(mopt)} }class myOperationThread : NSOperation {override func start(){super.start()}override func main(){for var i = 0; i < 100000; i++{println("NSOperation running.")}println("NSOperation over.")} }
調用:

var st = swiftThreadDemo()st.testNSThread()sleep(2)st.testGCDThread()st.testNSOperationQueue()

總結

以上是生活随笔為你收集整理的初探swift语言的学习笔记五(线程)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。