为什么“(UIButton *)sender”而不是“UIButton *sender”?
在 Objective-C 中,“(UIButton *)sender”是什么意思,为什么不是“UIButton *sender”?或者用一些 NSObject 代替 UIButton。这更多是关于星号优先级的问题......
- (IBAction)digitPressed:(UIButton *)sender {
//...
}
In Objective-C, what does "(UIButton *)sender" mean and why is it not "UIButton *sender"? Or some NSObject in place of UIButton. This is more a question about the precedence of the asterisk...
- (IBAction)digitPressed:(UIButton *)sender {
//...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,这与优先级无关。括号不是强制转换。
这是 ObjC 中方法声明的语法,它表示名为
sender
的参数的类型为UIButton *
。星号与 UIButton 一起出现,因为它们一起命名参数的类型。在本例中,由于它是来自按钮的操作方法,因此您使用的是 UIButton*。当然,在一般情况下,方法可以具有任何类型的参数,只要调用者正确调用它即可。 :)
It's not about precedence in this case. The parentheses aren't a cast.
This is the syntax in ObjC for a method declaration, and it says that the parameter called
sender
is of typeUIButton *
.The asterisk goes with the UIButton because they together name the type of the argument. In this case, since it's an action method coming from a button, you're using a UIButton*. In the general case, of course, a method may have parameters of any type, as long as the caller is calling it correctly. :)