Why does the compiler error not indicate the protocol, but fail to execute the required methods?

In objective-c, I can do something like

@interface MyViewController : UIViewController <UITextInputDelegate> 

to create a specification for the MyViewController class that implements the UITextInputDelegate protocol. Now this protocol has several necessary methods, so I think that the compiler will not allow compilation of code at all, unless these methods have implementations in the .m file. Sure, this compiles. The compiler generates warnings, so it’s obvious that I found that I did not use the required methods, but I wonder why it makes sense to allow this compilation in general from the side of the language developer.

+7
source share
3 answers

You get a compiler error when the code cannot be compiled. Method failure does not interfere with compiling the code, because objective-c is a dynamic language. This means that the methods are not directly related, so their location does not have to be known at compile time. Warnings mean that there is something that can cause runtime errors, but that the code has compiled successfully.

+9
source

As ughoavgfhw pointed out, this is not a mistake, because the dynamic nature of the language allows you to add these methods at runtime. Just because the method was not found at compile time does not mean that it will not be present at run time.

+2
source

If you want to turn this warning into an error, just add -Werror=protocol to Other Warning Flags in Build Settings .

enter image description here

+2
source

All Articles