Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 何时将冒号与@选择器一起使用_Ios_Objective C_Iphone_Selector_Nsnotifications - Fatal编程技术网

Ios 何时将冒号与@选择器一起使用

Ios 何时将冒号与@选择器一起使用,ios,objective-c,iphone,selector,nsnotifications,Ios,Objective C,Iphone,Selector,Nsnotifications,刚刚开始iPhone开发和Objective-C 昨天,我试图在视图中添加通知的Observer,我不断收到以下错误: unrecognized selector sent to instance 我得到了完全相同的错误: unrecognized selector sent to instance 已将无法识别的选择器发送到实例 有什么好处?为什么一个@selector需要尾随冒号,而另一个不需要?我应该遵守什么规则,什么时候应该包括它,什么时候应该不包括它,为什么我不能总是只做一个或另一个

刚刚开始iPhone开发和
Objective-C

昨天,我试图在视图中添加通知的Observer,我不断收到以下错误:

unrecognized selector sent to instance 我得到了完全相同的错误:

unrecognized selector sent to instance 已将无法识别的选择器发送到实例 有什么好处?为什么一个
@selector
需要尾随冒号,而另一个不需要?我应该遵守什么规则,什么时候应该包括它,什么时候应该不包括它,为什么我不能总是只做一个或另一个


谢谢

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

-(IBAction) doIt:(id)sender;
具有名称
doIt:
。因此,应使用冒号来引用此方法。
但是这个方法的结尾没有冒号

-(IBAction) doItWithoutParameter;

接受多个参数的方法也是如此,它们的名称类似于
doItWithParam1:andParam2:

,正如boltClock所提到的,您所指的字符实际上是冒号。
@selector(method)
@selector(method:)
之间的区别在于方法签名。第二个变量需要传递一个参数

@selector(method)
将期望方法:
-(void)method


@selector(method:)
期望方法:
-(void)method:(id)someParameter

冒号表示该方法采用参数

[someObject performSelector:@selector(doSomething:)]
表示doSomething需要一个参数

[someObject performSelector:@selector(doSomething)]
表示doSomething不需要任何参数。

在您的情况下:

[[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 */
}

我认为问题在于缺少参数


请参阅本文:(回答得很好!)

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

  • mySelector
    -无冒号,无参数,例如
    -(void)mySelector
    [自我选择]
  • mySelectorWithFoo:
    -一个冒号,一个参数,例如
    -(void)mySelectorWithFoo:(Foo*)Foo
    [self-MySelector WithFoo:someFoo]
  • mySelectorWithFoo:withBar:
    -两个冒号,两个参数,例如
    -(void)mySelectorWithFoo:(Foo*)Foo-bar:(bar*)
    [self-mySelector WithFoo:someFoo-bar:someBar]
  • 等等

    也可以在不“命名”参数的情况下使用选择器。不建议这样做,因为目前还不清楚参数是什么:

  • mySelector::
    -两个冒号,两个参数,例如
    -(void)mySelector:(Foo*)Foo:(Bar*)Bar
    [自我选择:someFoo:someBar]
  • mySelector::
    -三个冒号,三个参数,例如
    -(void)mySelector:(int)x:(int)y:(int)z
    [自我选择者:2:3:5]

  • 这是冒号,不是分号。无论如何,您的
    nameOfMySelector:
    方法的原型是什么?它想要什么样的论点?你在这两种情况下指的是同一种方法吗?哈!我的疏忽;我已经编辑了这篇文章,把它们称为冒号,并且我已经更正了标题。好了,就是这样。完全有道理。谢谢你的快速回答和语法课:)
    [[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 */
    }