8

In objective-c, I can do something like

@interface MyViewController : UIViewController <UITextInputDelegate>

to create the specification for a class MyViewController which implements the UITextInputDelegate protocol. Now, this protocol has several required methods, so I would think that the compiler would not let the code compile at all unless those methods did in fact have implementations in the .m file. Nonethless, this compiles. The compiler does spout warnings, so obviously it's detecting that I didn't implement the required methods, but I'm wondering why it makes sense to allow this to compile at all, on the part of the language designer.

1
  • Just because code compiles does not mean it cannot crash. The compiler will let all sorts of bugs through. A debugger is what you're after.
    – PengOne
    Jun 21, 2011 at 21:18

3 Answers 3

9

You get a compiler error when the code can't be compiled. Not implementing a method doesn't prevent the code from being compiled because objective-c is a dynamic language. That means that the methods aren't directly linked, so their location doesn't need to be known at compile time. Warnings mean that there is something which could cause errors at runtime, but that the code successfully compiled.

4

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

enter image description here

1
  • 1
    This is the useful answer
    – mistahenry
    Apr 17, 2019 at 14:00
2

As ughoavgfhw pointed out, it's not an error because the dynamic nature of the language allows for those methods to be added at runtime. Just because the method isn't found at compile time doesn't mean it won't be there at run time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.