Daniel
Daniel

Reputation: 6842

Implementing responder action in Swift

I am having difficulty implementing an action for an NSMenuItem in Swift. Normally, you implement the action like this in Objective-C:

- (void) asdf:(id)sender

This works perfectly fine, after setting up the action in the first responder like so:

user defined first responder

However, after rewriting my view controller in Swift, the following new method doesn't seem to be called:

func asdf(sender: AnyObject?)

It doesn't seem to work, even though both the Obj-C and Swift versions are for the same view controller subclass.

Upvotes: 0

Views: 389

Answers (1)

Krodak
Krodak

Reputation: 1493

In Swift 3.0 you'd define it as:

func asdf(_ sender: Any)

Why?

If you use _ you can drop parameter name when calling a function, so now you can call it like:

object.asdf(object)

Instead of:

object.asdf(sender: object)

Moreover, with Swift you'd use Any instead of AnyObject in this context. You can find more on differences between those here.

Upvotes: 1

Related Questions