2

I'm porting my project in Obj-C to Swift and I'm stuck in this method where I have:

textView: NSTextView, doCommandBySelector commandSelector: Selector) -> BOOL

in my objc code I had before

[textView performSelector:commandSelector withObject:nil];

but in Swift I have no idea about the solution, how to actually write the line of code.

Anyone have the solution for this?

I'm going crazy truly for the last week, even after reading tons of documentation.

Thanks

Leandro Papasidero
  • 3,728
  • 1
  • 18
  • 33
  • Swift views selectors as inherently unsafe, and as far as I know does not support the old "performSelector". There is a Selector type in Swift, but it's uses are somewhat limited. Look around stack overflow, and you'll see some discussion on the use of timers as a work around. – jwlaughton Mar 03 '15 at 03:59
  • See [@selector() in Swift?](http://stackoverflow.com/questions/24007650/selector-in-swift) – Aaron Brager Mar 03 '15 at 04:02
  • I already looked around stack overflow for a solution but I truly do not understand how to write that line of code in Swift. Do you know how maybe? – Davide Scheriani Mar 03 '15 at 05:19
  • I tried timers too, it works but when I hit the delete key to correct/delete, it goes against the 'complete:' method. – Davide Scheriani Mar 03 '15 at 05:43

2 Answers2

0

Even in Objective-C you have to use method -[UIApplication sendAction:to:from:forEvent:] for this so in Swift try somethings like this:

UIApplication.sharedApplication()
    .sendAction(commandSelector, to: textView, from: self, forEvent: nil)

More info on Apple Dev Forums.

Valentin Shergin
  • 7,166
  • 2
  • 50
  • 53
0

Future readers might like to know that performSelector and other members of the same family are available as of Swift 2.

TwoStraws
  • 12,862
  • 3
  • 57
  • 71