Monday, February 16, 2009

iPhone Development: A Self Directed Aproach

A few days back I saw a question on a forum saying "how do we send request to a http server from iPhone?". I instantly replied with an example which will send and process HTTP GET response from a server. I left a note at the end saying you can now read the documentation to do the rest. Guess what, the next question was "I have an xml how do I send it to server?".
All those who have been following forums for technical trouble shooting must agree with me that most of the questions asked these days are purely fixable if you go through the software documentation. But unfortunately many people are unaware of the benefits of making it a habit instead they prefer asking for the complete code which should do their job.
Let me assure you, once you develop a taste for reading documentation for everything you don't know, it will be a million times easy to solve problems. After all you don't have to wait for some1 to reply, besides you never know how bad practices get inherited from some stranger.
Here I am going to discuss some tips & tricks which will make your job much easier.
`
Lets start with a question. You are making a UITableView and you want that each cell has a disclosure indicator at the right side. You are doing this for the first time and you don't know that you have set the accessoryType variable to UITableViewCellAccessoryDisclosureIndicator. What will you do? There can be two approaches:
  1. Type some keywords on google and look through 3-4 links and you will find the solution.
  2. Refer the documentation.
But who wants to scan the crowded header files for the solution, when option 1 is just a 5-10 minutes job. I will tell you how you can do the job in less than 5 seconds.

Tip#1
I
n your code, type [cell setAccessoryType:] and press [Esc]. Yes here you will be able to see all the available types of Accessory types. Please note that you should place the cursor after the colon (:) and then press [Esc]. Now you see! In less then 5 seconds you not only found you solution but you also learnt about other types of CellAccessories.
Similarly you can do anything you don't know. For example you want to create a UIActivityIndicator. All you have to do is type:
UIActivityIndicator *spinner = [[UIActivityIndicator alloc] ] and press [esc]. All the options of init will be listed in the IDE. Now you want to start the spinner, then type [spinner ] and [ress escape. All the methods will be listed and among them one will be startAnimation. Easy na? Besides you also got to know that there is one stopAnimation method also. Just keep one thing in mind that you have to keep your cursor inside the rectangular brackets.
`
Tip#2
Let us now assume that you didn't even know if there is something called accessoryType in previous case but you have seen some where that cells can have different indicators on the right side. Now you don't have an option but to search google for the code or open the header files of UITableViewCell. Here I will tell you how you can quickly browse through the class references and header files. To open the reference window you can use the shortcut [ctrl]+[window]+[shift]+[?]. A window will open up. Now in the xCode if type UITableViewCell, the refrence window will automatically open heade file, class reference and guide. The refrence is very well structured in terms of tasks. So all you have to do is select a task and see the methods. Alternatively you can look at the header files for Declared Variables and structures, from there you will get instant access to what you want.
`
Although google remains the most popular source for code references, referring documentation saves a lot of time besides adding to your knowledge more than what you intended to.
Still it's a question open for debate, comments and suggestions will be highly appreciated.
`
In the next few posts I am going to discuss Introspection which is really a power full technique in objective c. You ever faced a problem writing loong parsers for every web service you implemented? I will tell you how you can write an xml parser in just TEN lines of code!

`
`