NSLog有可能成为objective-c选择器,即SEL类型的变量吗?

时间:2022-04-18 22:53:25

In some legacy code (yes, this is superceded by blocks now) I have the following lines:

在一些遗留代码(是的,现在已经被块取代)中,我有以下几行代码:

[UIWindow setAnimationDelegate:self];
[UIWindow setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];

but that method - animationFinished:finished:context: - is not defined, yet the code doesn't crash. I wanted to know why, so I tried to log what @selector returns, but I don't know how.

但是这个方法——animationFinished:finished:context:——没有定义,但是代码不会崩溃。我想知道为什么,所以我尝试记录@selector返回的内容,但我不知道如何做。

NSLog(@"%@", NSStringFromSelector(@selector(animationFinished:finished:context:)));

doesn't help - it will log the string name of the selector regardless of whether that method is actually implemented or not.

没有帮助——它将记录选择器的字符串名称,而不管该方法是否实际实现。

You can safely pass nil to many methods, so I presumed that @selector() was returning nil here, yet the following code doesn't log anything:

您可以安全地将nil传递给许多方法,因此我假设@selector()在这里返回nil,但以下代码没有记录任何内容:

if (@selector(animationFinished:finished:context:) == nil) {
   NSLog(@"SEL is NIL");
}

So it seems I have a non-nil SEL type here, yet no corresponding method. Is there a way to NSLog() a SEL type or can anyone explain what is going on here? Presumably, the implementation of setAnimationDidStopSelector: is able to cope with a SEL that has no corresponding method and just ignores it..

我这里有一个非nil的SEL类型,但没有相应的方法。有没有一种方法可以让NSLog()一个SEL类型或者谁能解释一下这里发生了什么?假设setAnimationDidStopSelector:的实现能够处理一个没有对应方法的SEL,并忽略它。

3 个解决方案

#1


1  

Yes, the selector, meaning the SEL object, may well exist even when the actual method has not been implemented.

是的,选择器(即SEL对象)很可能存在,即使实际的方法还没有实现。

In Objective-C an object may respond to any selector regardless whether it corresponds to a method or not. Validation of the selector is a run-time effort. (Some restrictions where introduced with ARC however)

在Objective-C中,对象可以对任何选择器作出响应,而不管它是否与方法相对应。选择器的验证是运行时工作。(有些限制是用ARC引入的)

If you want to check whether the receiver has it implemented (or more formally whether it responds to the selector or not) use its -(BOOL)respondsToSelector:(SEL)selector method.

如果您想检查接收方是否实现了它(或者更正式地说,它是否响应选择器),请使用它的-(BOOL)respondsToSelector:(SEL)选择器方法。

#2


4  

You're already using NSStringFromSelector which is the correct way to log the selector. Whether you create the selector yourself or ask the target class what selector it has is a different matter.

您已经在使用NSStringFromSelector,这是记录selector的正确方式。无论您自己创建选择器,还是询问目标类它有什么选择器,都是另一回事。

You can also use respondsToSelector: to determine if a specified class can actually handle the method you want to call on it.

您还可以使用respondsToSelector:确定指定的类是否能够实际处理要调用的方法。

#3


0  

Probably UIWindow checks if the selector is actually implemented in self with

可能是UIWindow检查选择器是否真的在self中实现。

- (BOOL)respondsToSelector:(SEL)aSelector

And if its not implemented it doesn't call the method.

如果它没有实现它就不会调用这个方法。

#1


1  

Yes, the selector, meaning the SEL object, may well exist even when the actual method has not been implemented.

是的,选择器(即SEL对象)很可能存在,即使实际的方法还没有实现。

In Objective-C an object may respond to any selector regardless whether it corresponds to a method or not. Validation of the selector is a run-time effort. (Some restrictions where introduced with ARC however)

在Objective-C中,对象可以对任何选择器作出响应,而不管它是否与方法相对应。选择器的验证是运行时工作。(有些限制是用ARC引入的)

If you want to check whether the receiver has it implemented (or more formally whether it responds to the selector or not) use its -(BOOL)respondsToSelector:(SEL)selector method.

如果您想检查接收方是否实现了它(或者更正式地说,它是否响应选择器),请使用它的-(BOOL)respondsToSelector:(SEL)选择器方法。

#2


4  

You're already using NSStringFromSelector which is the correct way to log the selector. Whether you create the selector yourself or ask the target class what selector it has is a different matter.

您已经在使用NSStringFromSelector,这是记录selector的正确方式。无论您自己创建选择器,还是询问目标类它有什么选择器,都是另一回事。

You can also use respondsToSelector: to determine if a specified class can actually handle the method you want to call on it.

您还可以使用respondsToSelector:确定指定的类是否能够实际处理要调用的方法。

#3


0  

Probably UIWindow checks if the selector is actually implemented in self with

可能是UIWindow检查选择器是否真的在self中实现。

- (BOOL)respondsToSelector:(SEL)aSelector

And if its not implemented it doesn't call the method.

如果它没有实现它就不会调用这个方法。