In Objective-C, is there a way to specify which selector definition you want to use @selector ()?

Xcode 4 gives me (rather useless) errors regarding the "unimplemented selector" xxx "when I try to use @selector (xxx) with any method not actually defined in the same source file. The error goes away (at least to build the project ), if I set the LLVM compiler warning β€œMultiple definition types for the selector” to β€œNo.” (This is the default value for iOS, but it was turned on for my project.) However, even with this, turning off the error appears in the editor if in the dialog the "Build options" window has the option "Enable live problems "strike">

So, now I turned off the live releases so as not to be distracted, which is a little let down. My question is: is there a way to get rid of the error, possibly by specifying which selector definition I want to use? Or is it even important, i.e. Do all method definitions have the same selector in Objective-C? Is this a compiler error, or perhaps a dummy setting, which I should just leave? (And if the latter, why is it enabled for the live build function in the new editor?)

Here is the code to be clear:

if ([recognizer respondsToSelector:@selector(translationInView:)]) { ... } 

And here is the error:

 error: unimplemented selector 'translationInView:' [-Wselector,2] if ([recognizer respondsToSelector:@selector(translationInView:)]) { ^ 

If I replaced 'translationInView:' with a method defined in the same source file, there is no error. I imported the header that defines this method, and I tried to declare the method in a category in this source file. Never mind.

I leave the warning off and the living builds and advances, but I would like to find the best solution for this problem. At the very least, I would like to know if Objective-C @selector has syntax for choosing a specific method definition, since I have not yet found any signs of this.

Thanks!

+6
source share
4 answers

Selectors have no relationship with definitions. At a basic level, this is truly a unique value that identifies the method name. The following methods have the same selector:

 - (void)doSomething:(id)foo; - (int)doSomething:(NSUInteger)i; - (void (*)())doSomething:(char *)name; 

All these methods have the same @selector(doSomething:) selector.

I believe the problem is when you refer to @selector(translationInView:) , the compiler tells you that it never saw a single method anywhere that this selector has, although I cannot be sure because you did not insert your exact error. You must make sure that the header file declaring this method is actually imported into your current file. If you cannot do this, you can always declare a method in a category in NSObject, for example:

 @interface NSObject (SelectorStuff) - (CGPoint)translationInView:(UIView *)view; @end 

This will tell the compiler that this selector exists, although it will also have a side effect, allowing you to call [foo translationInView:bar] on any object in this file without receiving a warning (of course, this will still work at runtime).

+5
source

From the Objective-C Language Guide :

Compiled selectors are of type SEL. All methods with the same name have the same selector.

...

For efficiency, ASCII full names are not used as method selectors in compiled code. Instead, the compiler writes each method name to a table, then matches the name with a unique identifier that represents the method at runtime. The runtime system make sure that each identifier is unique: no two selectors are the same, and all methods with the same name have the same selector.

So, as for the selection of selectors, the definition does not matter ... only the name of the method.

+1
source

If you use the GCC interface, you must set this flag to receive these warnings:

 -Wundeclared-selector 

Please note that this flag is not set by default, so you need to add it to the assembly configuration in some way so that you can see the warning.

0
source

Personally, I believe that this is a mistake in the compiler, because the error appears even when the selector is declared in a different category, in which case the compiler must safely assume that it is not implemented in this source file. The compiler should mark this as a possible problem that should be confirmed during the link, and only if there is no real implementation after all objects / libraries are connected, if it causes this warning.

0
source

Source: https://habr.com/ru/post/885843/


All Articles