何时将冒号与@selector一起使用
刚刚开始使用 iPhone 开发和 Objective-C
。
昨天,我试图在我的视图中添加通知以获取通知,但我不断收到此错误:
unrecognized selector sent to instance
我追踪到我需要将尾随冒号包含在我的选择器参数中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nameOfMySelector:) name:@"BBLocationServicesAreDisabled" object:nil];
今天,我认为我很聪明,因为在为按钮设置操作参数时,我想起了昨天的错误,并将冒号添加到了操作参数中。 action 参数采用 @selector ,就像为 NSNotification 设置观察者时的选择器参数一样,所以我认为我做的是正确的事情。
但是,使用以下代码:
[self.callToActionButton addTarget:self action:@selector(nameOfMySelector:) forControlEvents:UIControlEventTouchUpInside];
我得到完全相同的错误:
unrecognized selector sent to instance
什么给出了?为什么一个 @selector
需要尾随冒号,而另一个则不需要?我应该遵循哪些规则何时应该包含它以及何时应该删除它,以及为什么我不能总是只做其中之一?
谢谢!
Just getting going with iPhone development and Objective-C
.
Yesterday I was trying to addObserver for a notification in a view of mine, and I kept getting this error:
unrecognized selector sent to instance
I tracked it down to the fact that I needed to include the trailing colon to my selector argument:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nameOfMySelector:) name:@"BBLocationServicesAreDisabled" object:nil];
Today, I thought I was clever because when setting up the action argument to a button, I remembered my mistake yesterday, and added the colon to the action argument. The action argument takes a @selector
, just like the selector argument while setting up an observer for an NSNotification
, so I figured I was doing the right thing.
However, with the following code:
[self.callToActionButton addTarget:self action:@selector(nameOfMySelector:) forControlEvents:UIControlEventTouchUpInside];
I get the exact same error:
unrecognized selector sent to instance
What gives? Why does one @selector
require a trailing colon, and the other doesn't? What are the rules I should follow for when it should be included and when it should be left off, and why I can't I always just do one or the other?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如boltClock所提到的,您所指的字符实际上是一个冒号。
@selector(method)
和@selector(method:)
之间的区别在于方法签名。第二个变体需要传递一个参数。@selector(method)
期望的方法:-(void)method
@selector(method:)
期望的方法:-(void)方法:(id)一些参数
As mentioned by boltClock, the character you are referring to is actually a colon. The difference between
@selector(method)
and@selector(method:)
is the method signature. The 2nd variant expects a parameter to be passed.@selector(method)
would expect the method:-(void)method
@selector(method:)
would expect the method:-(void)method:(id)someParameter
您似乎在这里缺少一个概念:冒号在某种程度上是方法名称的一部分。例如,方法的
名称为
doIt:
。因此,应使用冒号来引用此方法。但此方法末尾没有冒号。
同样,接受多个参数的方法也适用,它们的名称类似于
doItWithParam1:andParam2:
You seem to be missing one concept here: colon is, in some way, a part of the method name. E.g., method
has name
doIt:
. Thus, colon should be used to reference this method.But this method doesn't have a colon at the end
Same goes for methods accepting multiple arguments, they have names like
doItWithParam1:andParam2:
选择器代表方法名称,选择器中冒号的数量与相应方法中参数的数量相匹配:
mySelector
— 无冒号,无参数,例如- (void)mySelector;
,[self mySelector];
mySelectorWithFoo:
— 一个冒号,一个参数,例如- (void)mySelectorWithFoo:(Foo *)foo;
,[self mySelectorWithFoo:someFoo];
mySelectorWithFoo:withBar:
— 两个冒号,两个参数,例如- (void)mySelectorWithFoo:(Foo * )foo bar:(Bar *)bar;
、[self mySelectorWithFoo:someFoo bar:someBar];
等等。
也可以有一个选择器而不“命名”参数。不推荐这样做,因为不能立即清楚参数是什么:
mySelector::
— 两个冒号,两个参数,例如- (void)mySelector:(Foo *)foo :(Bar *) bar;
,[self mySelector:someFoo :someBar];
mySelector:::
— 三个冒号,三个参数,例如- (void)mySelector :(int)x :(int)y :(int)z;
,[self mySelector:2 :3 :5];
A selector represents a method name, and the number of colons in a selector matches the number of arguments in the corresponding method:
mySelector
— no colon, no arguments, e.g.- (void)mySelector;
,[self mySelector];
mySelectorWithFoo:
— one colon, a single argument, e.g.- (void)mySelectorWithFoo:(Foo *)foo;
,[self mySelectorWithFoo:someFoo];
mySelectorWithFoo:withBar:
— two colons, two arguments, e.g.- (void)mySelectorWithFoo:(Foo *)foo bar:(Bar *)bar;
,[self mySelectorWithFoo:someFoo bar:someBar];
and so forth.
It is also possible to have a selector without ‘naming’ the parameters. It’s not recommended since it’s not immediately clear what the parameters are:
mySelector::
— two colons, two arguments, e.g.- (void)mySelector:(Foo *)foo :(Bar *)bar;
,[self mySelector:someFoo :someBar];
mySelector:::
— three colons, three arguments, e.g.- (void)mySelector:(int)x :(int)y :(int)z;
,[self mySelector:2 :3 :5];
冒号表示该方法需要一个参数。
[someObject PerformSelector:@selector(doSomething:)]
表示 doSomething 需要一个参数。[someObject PerformSelector:@selector(doSomething)]
表示 doSomething 不需要任何参数。The colon indicates that the method takes a parameter.
[someObject performSelector:@selector(doSomething:)]
means that doSomething is expecting a parameter.[someObject performSelector:@selector(doSomething)]
means that doSomething doesn't need any parameters.在你的情况下:
或者在这种情况下:
In your case:
or in this case:
我认为问题在于缺少参数。
请参阅这篇文章:Objective-C:使用多个参数调用选择器< /a>(很好的答案!)
I think the problem is the missing parameter.
See this post: Objective-C: Calling selectors with multiple arguments (Great answers!)