Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autodetect IBAction and notification handlers #179

Open
charlieMonroe opened this issue Jan 22, 2016 · 2 comments
Open

Autodetect IBAction and notification handlers #179

charlieMonroe opened this issue Jan 22, 2016 · 2 comments

Comments

@charlieMonroe
Copy link

Hi, hope you feel better!

I was wondering if it was possible to detect 2 types of methods:

  • If the method has 1 argument, whose name is sender of type id (or AnyObject in Swift), it would be cool to automatically mark this method as @IBAction.
  • If the method has 1 argument, name doesn't matter, but the type is NSNotifications, make it @objc.

There are many cases where I have forgotten about adding this attribute and then the app crashes when you try to invoke the action or the object receives the notification.

@drkameleon
Copy link
Collaborator

Hmm... This sounds very interesting. Feel free to post any such cases (and/or with examples) and I'll see what I can do.

In any case, it sure makes sense.


P.S. I'm better though - what can I say - 2016 has already made a strong... impression on me! haha. I don't know what's going on in general, but around here the flu wave is rather... massive...

@charlieMonroe
Copy link
Author

Here are some examples:

-(void)_applicationWillTerminate:(NSNotification *)aNotif {
  // Do something.
}

// This is usually marked as -(IBAction) in the header file, while in the .m file, 
// this is usually void since IBAction is just #define IBAction void.
// sender is id here since we don't need it or it can be called e.g. from both a button
// and a menu item.
-(void)showHelp:(id)sender {
   // Do something
}

-(void)save:(NSButton *)sender {
   // Do something
}

This should become ideally this:

// Needs to be @objc since it's called via objc_msgSend.
@objc func _applicationWillTerminate(aNotif: NSNotification) {

}

// The same reasoning as before. @IBAction implies @objc, but
// also allows IB to "link" the action. Several notes:
// - sender is optional as per conventions
// - lately, when you create an action from Interface Builder, it allows you to specify
//   the object type - e.g. NSButton. In such case the sender should not be optional.
//   See the other example.
@IBAction func showHelp(sender: AnyObject?) {

}

// Sender is of a specified type, so it should not be optional and the type
// should be kept as-is.
@IBAction func save(sender: NSButton) {
   // Do something
}

Hope it makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants