3
import UIKit


class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @objc class myClass:NSObject {

        func myFunc(){
            for var i = 0; i < 10000; i++ {
                print(i,[NSThread.currentThread()]);
            }
        }
    }
    var myObj = myClass()

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        _ = NSThread.init(target: myObj, selector:Selector(myObj.myFunc()), 

    object: nil)
        }
    }

when I run the code above, Xcode has a exception.the information is

[NSThread initWithTarget:selector:object:]: target does not implement selector

how to use performSelectorInBackground in swift ??

gagan mahatma
  • 336
  • 2
  • 9
yang
  • 31
  • 1
  • 5
  • `Selector(myObj.myFunc())` will map to a selector corresponding to the return value of `myFunc()`, which is `()`, not to the actual function. – Cristik Jan 07 '16 at 06:50

6 Answers6

2

Simple answer: You don't. Use dispatch_async.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • 2
    This. The whole `performSelector...` thing is a relic from Objective-C and dynamic dispatch. Moving away from this construct and adopting more modern, agnostic alternatives like GCD ensures better quality Swift code. – Nicolas Miari Jan 07 '16 at 09:51
1

A Selector is associated with the name of a method represented as a String. You're not properly initializing the Selector. Selector has an initializer that takes a String, so you shouldn't be passing it myObj.myFunc(). Instead you should be passing it "myFunc". See this question for more detail: @selector() in Swift?

Community
  • 1
  • 1
davecom
  • 1,499
  • 10
  • 30
  • change the code to `_ = NSThread.init(target: myObj, selector:Selector("myFunc"), object: nil)` ? – yang Jan 07 '16 at 05:59
0

You can use perform selector as below:

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    _ = NSThread.init(target: myObj, selector:Selector("myFunc"),

        object: nil)
}

To perform some task in background you can use dispatcher as an alternative for performSelectorInBackground.

let backgroundQueue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)
    dispatch_async(backgroundQueue) { () -> Void in

        self.myFunc()

    }
technerd
  • 14,144
  • 10
  • 61
  • 92
  • **thanks a lot**.I found we can also use **performSelectorInBackground** in function. – yang Jan 07 '16 at 06:17
0

Two points need to pay attention: first, the class which use performSelectorInBackground must inhert NSObject. second,performSelectorInBackground(Selector("myFunc"), withObject: nil) is correct. myFunc is function name.

yang
  • 31
  • 1
  • 5
0

This Worked for me

performSelector(inBackground: #selector(Selection_Button(url:)), with: finalPath2)
Guri S
  • 1,083
  • 11
  • 12
0

Note the latest syntax for performing a task in the background is:

let backgroundQueue = DispatchQueue.global(qos: .background)
backgroundQueue.async {
    // Do stuff in the background
    DispatchQueue.main.async {
        // Pass to the main queue for UI work etc
    }
}

If you're referring to self in the background queue you probably want to add [weak self] in after the .async {.

JonJ
  • 1,061
  • 6
  • 8