Creating threads in swift?

zumzum picture zumzum · Jul 2, 2014 · Viewed 14.4k times · Source

I am trying to spawn a thread in swift. So I have this line:

. . .

let thread = NSThread(target: self, selector: doSomething(), object: nil)

. . .

doSomething is a function within the scope of the class.

That line gives this error: "could not find an overload for init() that accepts the supplied arguments"

What am I missing here? Ho can I create a new thread in swift?

Answer

rob mayoff picture rob mayoff · Jul 2, 2014

As of Xcode 7.3 and Swift 2.2, you can use the special form #selector(...) where Objective-C would use @selector(...):

let thread = NSThread(target:self, selector:#selector(doSomething), object:nil)