4. Understanding the difference between the .h file and the .m file.

So if you are still with me, you are in Xcode looking at the left side of the screen, wanting to click on the ExampleViewController, but you notice there are two files with that same name! What I am about to discuss is very important to understand, so read carefully! The two files of the ExampleViewController have different and unique roles. When considered together, both of those files constitute what is known as a Class. That's right a class. A gathering of students to hear a lecture type of class? No.

Whatever concept you have in your mind of the word class, throw it out the window right now. I am not going to describe in detail in this post how we use classes, but you need to start getting used to the terminology. When I talk about the ExampleViewController class, I am referring to the ExampleViewController.h and ExampleViewController.m files. Together they make up the ExampleViewController class. Pound that term into your head because it isn't going to go away.

So you might be wondering, what is the difference between the .h and the .m file?

Well, the things that you do in the two files are different but complimentary. The .h file refers to the fact that it is the header file, where certain things about the class (as a whole) are declared and detailed. The .m file implies that it is where the implementation of the class takes place, or in other words, it is where you do things with the variables you have declared in the .h or header file for the class.

It won't be beneficial to discuss much more detail at this point but it is important to note that these two files work together. As we move along, we will keep coming back to the purposes of the two files and you will see very clearly how they work together.

To start off our coding expedition though, we are going to jump straight to the implementation file. Which one is that? The ExampleViewController.m file.

Stay tuned for the next step in the process of building a firm iOS Foundation.


2 comments:

  1. Super helpful info, thanks Wes! It makes a lot more sense now. If these files constitute a class, is there a main method that we create to call them?

    ReplyDelete
  2. Hey Mac!

    Glad to see you made it to the site. I am so sorry that it took me so long to get back to you.

    You bring up a great question here. If you look inside of a standard Xcode project, you will see a yellow folder called Supporting Files. Within this group, as they are called, you will see a file called main.m. This is the main method. The life cycle of an app can be a tricky thing to nail down, but just know that the main method is called, and that you will most likely never need to do anything with it.

    After calling the main method, control continues on into the App Delegate where the App's Window is setup, and the first view is presented, at which point you move on into the view controller. There is never any need to call or reference main thereafter as there are a number of delegate methods that provide similar functionality throughout the lifecycle of the app.

    Hope that helps a little. Keep going through the posts and let me know what you think!

    ReplyDelete