I am reading about iOS programming and I bought the Programming iOS 4 book. There is a introductory part where among several things “Files” is mentioned.
I don’t understand how the source files is put together. You have a header file with function declarations, then you have a corresponding file with the function definitions.
Let say you have a Car.h and Car.m & Person.h and Person.m.
Now, if you want to use the Car in the Person class you would import only the Car.h file. How is this sufficient? I don’t understand the sequence it put together and builds a program. (Not thinking about the technical stuff, just h/m files.)
.hor “header file” contains the interface..mor “implementation file” contains the implementation.Each implementation file is also called a “compilation unit” because the compiler compiles each one separately. Within each compilation unit, the compiler needs to know about types and methods. All it needs to know about a class to create the right code is information about the methods it implements.
So let’s imagine you have these files:
Car.hCar.mPerson.hPerson.mNow when the compiler does its business, it compiles both
Car.mandPerson.mintoCar.oandPerson.orespectively. [These then get linked into the final binary, but that’s beyond the scope of this question for now].When it compiles
Person.m, the compiler doesn’t need to know how- (void)driveofCaris implemented, but it does need to know that it exists, that it is a method that takes no arguments and returns nothing. It doesn’t care about the implementation, just that it exists. So you just need to#importthe header file ofCarto tell the compiler about the methods that exist onCar. The compiler knows that the implementation exists, because you’ve told it so, and then later on the linker will do it’s business to correctly wire up the method call to the correct implementation. How the linker actually does that is a huge topic and I encourage you to go and read about it separately if you don’t already understand it.Note that it’s the same for all of the standard
NSclasses that you use such asNSObject,NSString, etc. You just need to#importFoundation.hfrom theFoundationframework which tells the compiler about what these classes are and what methods are defined on them.