I just started reading my first Objective-C book and it’s mostly pretty clear, however I want to understand why it teaches me to declare a method in the interface first, I’ve tried deleting it from the interface and just doing it in the implementation section and all seems to work so my question is does it matter and if so, what does this help to accomplish?
I just started reading my first Objective-C book and it’s mostly pretty clear, however
Share
It’s C. Declaring functions/methods in a header allows other compilation units to see your function prototypes and compile based on them. Objective-C works via dynamic dispatch though, so even if you don’t declare a method in the header, it still exists at runtime. When you call a method, it is resolved at runtime, so it doesn’t matter if it was in the header or not.
The only issue is, if you don’t include the method in the header, the compiler has to make assumptions about the return and argument types. It defaults to
id, which is 4 or 8 bytes depending on your architecture, and therefore you will get into trouble if the actual return type is of a different size (e.g. structs orBOOLetc).