何时将冒号与@selector一起使用

发布于 2024-10-16 11:37:08 字数 802 浏览 3 评论 0原文

刚刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

东北女汉子 2024-10-23 11:37:08

正如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

物价感观 2024-10-23 11:37:08

您似乎在这里缺少一个概念:冒号在某种程度上是方法名称的一部分。例如,方法的

-(IBAction) doIt:(id)sender;

名称为doIt:。因此,应使用冒号来引用此方法。
但此方法末尾没有冒号。

-(IBAction) doItWithoutParameter;

同样,接受多个参数的方法也适用,它们的名称类似于 doItWithParam1:andParam2:

You seem to be missing one concept here: colon is, in some way, a part of the method name. E.g., method

-(IBAction) doIt:(id)sender;

has name doIt:. Thus, colon should be used to reference this method.
But this method doesn't have a colon at the end

-(IBAction) doItWithoutParameter;

Same goes for methods accepting multiple arguments, they have names like doItWithParam1:andParam2:

把回忆走一遍 2024-10-23 11:37:08

选择器代表方法名称,选择器中冒号的数量与相应方法中参数的数量相匹配:

  1. mySelector — 无冒号,无参数,例如 - (void)mySelector;, [self mySelector];
  2. mySelectorWithFoo: — 一个冒号,一个参数,例如 - (void)mySelectorWithFoo:(Foo *)foo;, [self mySelectorWithFoo:someFoo];
  3. mySelectorWithFoo:withBar: — 两个冒号,两个参数,例如 - (void)mySelectorWithFoo:(Foo * )foo bar:(Bar *)bar;[self mySelectorWithFoo:someFoo bar:someBar];

等等。

也可以有一个选择器而不“命名”参数。不推荐这样做,因为不能立即清楚参数是什么:

  1. mySelector:: — 两个冒号,两个参数,例如 - (void)mySelector:(Foo *)foo :(Bar *) bar;, [self mySelector:someFoo :someBar];
  2. 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:

  1. mySelector — no colon, no arguments, e.g. - (void)mySelector;, [self mySelector];
  2. mySelectorWithFoo: — one colon, a single argument, e.g. - (void)mySelectorWithFoo:(Foo *)foo;, [self mySelectorWithFoo:someFoo];
  3. 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:

  1. mySelector:: — two colons, two arguments, e.g. - (void)mySelector:(Foo *)foo :(Bar *)bar;, [self mySelector:someFoo :someBar];
  2. mySelector::: — three colons, three arguments, e.g. - (void)mySelector:(int)x :(int)y :(int)z;, [self mySelector:2 :3 :5];
×眷恋的温暖 2024-10-23 11:37:08

冒号表示该方法需要一个参数。

[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.

肩上的翅膀 2024-10-23 11:37:08

在你的情况下:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nameOfMySelector:) name:@"BBLocationServicesAreDisabled" object:nil];

- (void) nameOfMySelector: (NSNotification *) notification {
    /* this method would require the semi-colon */
}

或者在这种情况下:

[self.callToActionButton addTarget:self action:@selector(nameOfMySelector:) forControlEvents:UIControlEventTouchUpInside];

- (void) nameOfMySelector: (id) sender {
    /* this method would also require the semi-colon */
}

In your case:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nameOfMySelector:) name:@"BBLocationServicesAreDisabled" object:nil];

- (void) nameOfMySelector: (NSNotification *) notification {
    /* this method would require the semi-colon */
}

or in this case:

[self.callToActionButton addTarget:self action:@selector(nameOfMySelector:) forControlEvents:UIControlEventTouchUpInside];

- (void) nameOfMySelector: (id) sender {
    /* this method would also require the semi-colon */
}
聆听风音 2024-10-23 11:37:08

我认为问题在于缺少参数。

请参阅这篇文章:Objective-C:使用多个参数调用选择器< /a>(很好的答案!)

I think the problem is the missing parameter.

See this post: Objective-C: Calling selectors with multiple arguments (Great answers!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文