2015-08-15 49 views
2

我試圖在迅速和按鈕點擊開始創建一個簡單的線程,但它拋出一個錯誤創造了快捷簡單的線程給出錯誤

"**Cannot find an initializer for type 'NSThread' that accepts an argument list of type ('target:ViewControllerm->())'** 

這裏是我的代碼:

import UIKit 

class ViewController: UIViewController { 

    var isSet = true   
    let thread123 = NSThread(target: self, selector: "myFunc", object: nil) 

    func myFunc() { 
    } 

    @IBAction func btnClickEvent(sender: AnyObject) { 
     // starting thread 
     thread12.start() 
    } 

} 

我在這裏做錯了什麼?

回答

4

錯誤消息是相當混亂。請嘗試重寫它作爲

let thread123:NSThread 

init() { 
    thread123 = NSThread(target: self, selector: "myFunc", object: nil) 
} 

,你會得到您顯示自現在還沒有

SO更清潔的錯誤消息:

let thread123:NSThread 

init() { 
    super.init() 
    thread123 = NSThread(target: self, selector: "myFunc", object: nil) 
} 

現在常數不調用初始化之前超..也NOGO

所以

var thread123:NSThread! 

init() { 
    super.init(nibName: nil, bundle: nil) 
    thread123 = NSThread(target: self, selector: "myFunc", object: nil) 
} 

或短暫的甜蜜

lazy var thread123:NSThread = NSThread(target: self, selector: "myFunc", object: nil) 
+0

感謝..這也適用.. – cybergeeeek

2

嘗試更換此行

let thread123 = NSThread(target: self, selector: "myFunc", object: nil) 

lazy var thread123:NSThread = 
    { 
     return NSThread(target: self, selector: "myFunc", object: nil) 
    }() 
+0

thanks..it工作 – cybergeeeek