A beginner here, developing for the iPhone, with an extremely simple question: What is the reason behind declaring a method in a header file, and then filling it out in the implementation file? Does this always have to be done?
Also, I know to declare variables in the @interface of the header file, but why are they only sometimes repeated with the @property tag? Is this for variables that other classes may wish to read from or write to (hence why they automatically create getter and setter methods)?
Kind regards.
Declaring a method in .h file is known as forward declaration. If you declares a method header in the .h file then the compiler will know the method name, parameters and return type before the actual linking. You can write a method body only in the .m file. But that method can be used only by methods declared after it in that file. But if you declare a method in the header then it won’t be a matter. Because then the method signature will be know to all during the first pass of compiler and will be linked in the second pass.
@propertyand@synthesizetags are used to create automated getters and setters (orAccessorsandMutators, inObjective-Cterminology), but there is more. IniOSyou have to do memory management manually (it should be changed iniOS5, as apple promised). In the@propertytag you can tell how the memory will behave during an assignment.iOStracks the memory management of an object by maintaining a retain count. When you allocate an object it’s retain count becomes 1. Then you can manually increase the retain count by the retain method (e.g.[myObj retain]) or decrease the retain count by the release method (e.g.[myObj release]). When the retain count falls to 0iOSdelete that object from memory. With the@propertytag you can define how retain count will be manage during an assignment. For example two mostly used parameters in the@propertytag are:In the first case during an assignment the retain count of the object will be increased by 1 automatically (e.g.
self.myObj = anotherClass.anotherObjOfSameClass;) and in the later case the retain count would not be increased.