iOS:在 IBAction 声明中包含发件人?
我有一个最佳实践/风格问题。假设我有一个 IBAction 方法,并且不需要 sender 参数(如果确实需要,我知道如何使用 sender 参数)。人们是否建议将 (id)sender 保留在方法声明中以保持一致性,或者为了简洁而将其排除?从功能上讲,这里没有正确的答案,只是好奇人们认为可维护性/同行评审等的最佳实践是什么。谢谢。
选项 A:
-(IBAction)foo:(id)sender;
选项 B:
-(IBAction)foo;
I have a best practices/stylistic question. Let's say I have an IBAction method and I have no need for the sender parameter (I'm aware how to use the sender parameter if I do need it). Do folks recommend leaving the (id)sender in the method declaration for consistency, or exclude it for brevity? Functionally, there is no right answer here, just curious about what folks consider best practice for maintainability/peer review/etc. Thanks.
Option A:
-(IBAction)foo:(id)sender;
Option B:
-(IBAction)foo;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通常将“发件人”留在那里。您现在可能不需要发件人,但它相当常见,以后可能需要它,因此不必返回并将其重新添加到其中,而是很容易将其保留在其中(无论如何它都会以这种方式自动完成)。它绝对不会伤害任何东西。
I generally leave the "sender" in there. You might not need the sender now, but it is fairly common and might need it later, so instead of having to go back and add it back in, it is easy enough to just always leave it in (it autocompletes that way anyway). It definitely doesn't hurt anything.
最好包含 sender 参数,即使您不需要它。原因之一:文档说您必须符合这个和那个参数列表。另一个原因是:如果您以后需要它,最好拥有它。
It's good practice to include the sender parameter, even if you don't need it. One reason: docs say you must conform to this and that parameter list. Another reason: if you'll need it later, it's good to have it.
我总是添加发件人,主要是为了保持一致性。另外,因为我通常不使用 IB,所以很明显哪些方法是操作方法
I always add the sender, mainly for consistency. Also because I generally don't use IB it makes it obvious which methods are action methods