- The iOS Beginner blog series
- PHP to Objective C, where the f**k are parameters?
- PHP/Rails to iOS, what are .h and .m files?
- Understanding M-VC in iOS
Since I began my iOS beginner blog series, I have been receiving tremendous feedback and appreciation from readers all over the world and lots of encouragement for me to continue with the series.
I started realizing that there are so many more developers out there who are struggling with iOS than I originally thought. So allow me to make my point very clear that this blog series is meant for beginners, sometimes certain statements might not be technically correct, but I intentionally wrote them in a certain way so as to simplify the thought process for new readers. Those ‘mistakes’ will not do any harm, as long as you keep progressing, the more advanced concepts will help you redefine those yourselves. In this post, I will make some of those in the process of explaining the concepts. This post is a highly simplified and short explanation for absolute begineer who have not touched any compiled language before iOS development.
The big question, unanswered
Just like you, I stumbled right at the first step of understanding iOS source code. With no background in compiled languages, I can’t really tell what are the purposes of different files. Java programmer would have no problem understanding all the declarations like @interface, @protocol, @implementation. (Don’t worry, my next post will talk about these in more details). PHP programmers, well, everything ends with .php
Unfortunately, the question “What are .h and .m files?” that I seeked is too fundamental that seasoned developers don’t really bother answering. I found some really short answers for this question here and here but they didn’t really help me much.
There are many many more questions like this along my journey of learning iOS. Until now I’m still puzzled why there aren’t many people that could help guiding beginners in the transition from web languages to compiled Objective-C language like this. StackOverflow is an excellent repository of even weirdest questions, but there ain’t many basic concepts explained well. Having to gone through the pain, now I am hoping to demystify some of these concepts for the absolute noobs like I was.
So what are .h and .m files?
.h file is header file, .m file contains the implementation
Each class in Objective C, usually made up by one .h file and one .m file.
The .h files are header files. It is called header file because it only contains the ‘head’ or a class, in other words, all the properties and functions that any other outside class should know about, the ‘public face’ of a class. It does not contain implementation logics at all.
On the other hand, .m file contains all the implementation details of all the functions declared in .h and more. It also contains declaration and implementation of private functions that outside classes cannot use, or should not care about.
How to ‘use’ these files, a simplified perspective
You got yourself a pair of .h and .m files, downloaded from Internet or someone send it to you and tell you to figure it out by yourself, either way you only have the files and no documentation. So you wonder where to start to get a big picture of what it does. This happens more often than you think. As a more seasoned developer now, I still find myself downloading open source projects and look at how some of the classes are implemented all the time, mostly from cocoacontrols.
First, you need to tell the names and parameters of functions. This is where my previous post came in very handy.
Secondly, you should start to *only* look at the .h file. Because this file contains the public interfaces of a class that the writer wants others to know. There are a lot of things inside a .h file but most important are the functions. They look like these:
// Convenience constructor + (id)requestWithURL:(NSURL *)newURL; + (id)requestWithURL:(NSURL *)newURL usingCache:(id <ASICacheDelegate>)cache; + (id)requestWithURL:(NSURL *)newURL usingCache:(id <ASICacheDelegate>)cache andCachePolicy:(ASICachePolicy)policy; // Run a request synchronously, and return control when the request completes or fails - (void)startSynchronous; // Run request in the background - (void)startAsynchronous; // Clears all delegates and blocks, then cancels the request - (void)clearDelegatesAndCancel;
This is taken direct from ASIHTTPRequest network library that I used for my latest projects.
And as you can probably tell, the snippet describes how to create a connect, start it and then stop it. It is pretty much all the basics that you need to make use of the class. Luckily a lot of library developers out there do include one-line comments like this in their public interface, they are really useful.
You only need to look at .m files if you want to understand or copy the implementation of the classes.
The import statements
At the beginning of all the files, you will need to import the .h files that you are working with. This is because Object-C is a compiled language, you need to tell the compiler what are the objects that you will be working with, more importantly the compiler need to know the exact interfaces available in those files in order to compile them in your current class. If you manage to compile successfully code that contains a call to a mis-spelt function name, your app will crash! (There are ways to do this).
At this point, you only need to know that whenever you need to call external classes, you need to import their .h files.
So now you can walk to your nearest iOS noob friend and pretend that you know everything about the class that he is working on just by looking at the .h file and quoting some function names. What’s cooler is that you can even ask why not declare them slightly different… that is before he shoots you down with questions you don’t even understand.
You can now subscribe to my blog using the form on the right column. Thanks for reading, do share it with your fellow iOS coder too.