Click here to Skip to main content
15,860,859 members
Articles / Mobile Apps / iPhone

Preparing the ground for iPhone development

Rate me:
Please Sign up or sign in to vote.
4.80/5 (14 votes)
10 Sep 2010CPOL8 min read 57.5K   819   37   4
An article for developers new to Apple technologies.

Introduction

This article is for someone who, like me, has coded before -- but never for Apple technologies.

A good friend gave me an iPhone primer book. So I installed Xcode and dived in. It has been a rocky six weeks. While all of the problems I have had to deal with are still fresh in my head, I will try to make the path a little easier for the next traveler. If you, like me, lack patience, this guide is for you.

I see a lot of books reinventing the wheel by writing their own Hello World applications, encouraging the reader to depend on the book. This is naughty. Some of the Apple documentation is very good!

Resources

  • Register with Apple ( 100 bucks )
    First of all, register as an Apple developer on their website http://developer.apple.com/. Once in, you have access to their developer videos, you can download and install Xcode, you have access to their forums.
  • IRC -- irc.freenode.net#iphonedev
    This channel is very active -- ask a good question and they will help you. as with any IRC chat room, asking a question without checking the obvious places first will not endear you to the community. (www.mibbit.com if you don't have a client).
  • Stack Overflow
    Is it a wiki? is it a forum? No, it is... Stack overflow! A groundbreaking new technology with very rapid turnaround -- often your questions will get answered almost as fast as asking on IRC.
  • Apple's doc
    ...of which Xcode downloads and keeps a local copy.
  • Vidz!
    Stanford University -- CS 193P iPhone Application Development
    All of these 20 lectures are available on video, though not from that site (but DO get the source code from that site!). You can get them from iTunes -> iTunesU -> ...(then you have to hunt for it). I recommend downloading all of these videos and flicking through them every time you have a spare half-hour. I like to skip to the end part where he puts it all together with a sample application, and then move back through the video if I am curious enough about anything I saw. My concentration span gives out if I just try to watch them all the way through.

    WWDC09/10
    Similarly for the WWDC09/10 videos on the Apple developer site. I recommend downloading everything and skipping through, letting the PowerPoint slides catch your eye, and you will gradually build your neural matrix. These videos are mainly for people already fluent with the technology, but some of them are designed for getting started.

  • Use the Source, Luke
    create a folder somewhere .../source/samples/ and into it download lots of juicy source code.

    Apple developer documentation samples
    there are hundreds of samples (100? 1000? I forget) on the Apple developer site. Some of them are listed, but some of them aren't! and you only find them by random chance on Google, or by pulling up the help screen on some keyword from within Xcode, and low and behold it has something under ' related sample code '.

    WWDC10 source code
    Can't remember off the top of my head how to get this -- Google knows!

    GitHub
    Again, thousands of projects people have open sourced. unfortunately there is no tidy way to browse through these projects, the best you will get his throwing 'iphone' into the search field and looking through 1000 or so entries. I really hope that somebody makes a wiki for organising these, and eventually they become more accessible.

  • Google & Blogs
    I recommend some GoogleFu to look for getting started links: here is one: http://stackoverflow.com/questions/1939/how-to-articles-for-iphone-development-objective-c Once you have got your teeth into it, probably about three months down the line (which is where I am at now) you will probably want to start following all of the top bloggers & get twitter so you get notified whenever one of them tweets out a new article. because you will always be stuck somewhere, and generally someone has already been stuck there and figured it out and wrote it up. http://stackoverflow.com/questions/232570/what-are-the-best-cocoa-touch-iphone-programming-blogs is a list for examples. Is missing some great ones, and contains some awful ones. So, use your discrimination.
  • Books
    Also, if you are new to coding, you need to go: C -> ObjC -> iPhone programming

    Bookwise, that translates as:

    The C Programming Language , the C Bible, written by Dennis Ritchie -- the guy that wrote C. Everyone who has read it tells everyone else to read it. I read it. Read it!

    Programming in Objective-C 2.0 appears to be the Bible for Objective-C.

    The iPhone Developer's Cookbook: Building Applications with the iPhone 3.0 SDK by Erica Sadun.

Xcode's Documentation - Setting Some Bookmarks

This documentation is a bit of a mess -- despite it physically being installing onto your hard drive when you install Xcode, Xcode cannot find this version, and instead tries to download the latest help files which are 300 megs. You can see this in action: Xcode -> Preferences -> Documentation. If you have a slow Internet pipe, that's just too bad -- these downloads don't seem to support resuming. I had to leave my MacBook at the local Internet cafe overnight.

Let's take a moment to customise help ...

From Xcode, Help -> Developer documentation should take you to the quick start page; bookmark it, as it's easy to lose it.

Image 1

See if you can find a local copy of the Hello World tutorial. Here is the web-based version, at the time of writing: Your First iOS Application.

If your brain works like mine and jumps all over the place, you may miss the home pull down.

Image 2

You can see the Hello World app guide highlighted there. Bookmark it.

I recommend you also find the Objective-C guide, and bookmark that. Notice that I have restricted the doc set, otherwise it throws up too much junk. At the bottom of this primer is a link to a full Objective-C reference. If you are new to Objective-C, it's worth skimming this so that you know where to look up weird looking syntax.

Image 3

As a last bookmark for now, find the memory management guide. Don't bother reading it all for now -- just have it ready so that when your code crashes in two weeks' time, with a totally unhelpful 'bad access' error, you can navigate to the cardinal rule:

Image 4

Four bookmarks -- that's enough for now!

A Little Preliminary Reading

Next, I recommend looking through the videos in the QuickStart -- that shows you how to use context sensitive help etc., and having a little skim of the Objective-C guide -- if you don't know any Objective-C, you will just get confused by the tutorial. If you are new to Objective C, you need to know: it is an extension of C. You can write in C code all the way if you want. But then you would waste thousands of hours of recoding classes to draw text on the screen that Apple has already written in their SDK. In fact, you can write C++. You can mix all of the different dialects of C and C++ with Objective-C.

Hello World Tutorial

Next, crack open the Hello World tutorial (the second link we created) and go through it. It's very good. For the most part. Not entirely -- it creates confusion in the way it names properties. I have corrected this -- you can download my version of this Hello World project from the link above.

But before I discuss the changes I have made, I want to make a quick point about colour schemes.

Colour Schemes

I recommend setting a good colour scheme that lets you see what is what. For example, rather than simply trying to get something looking pretty, I have clustered user defined objects into the red orange yellow spectrum, with the standard objects in the blue purple end. Here's a screenshot of what I'm using:

Image 5

You can see that vc is my own custom class I have created, coming up in red. UIWindow is one of Apple's classes. Instance variables show up in yellow.

Image 6

Here you can see: for Apple's classes, purple methods come from blue classes. For my classes, orange methods come from red classes. [vc alloc] has red vc, since I created that class, but I inherited it from NSObject (which almost every class should do). Hence alloc is in purple -- it is a method of NSObject.

Feel free to download my colour scheme (from the link above) and stick it in ~/Library/Application Support/Xcode/Color Themes (you will probably need to create a folder) and Xcode -> Preferences -> Fonts and colours -> Colour scheme should pick it up. But I do recommend you go there first and look at your options.

Properties

I will simply list the two relevant source files, as the code is commented generously. I prefer to comment the code, rather than discuss in the article, as it lets you step through and read without referring back and forth. In a nutshell, the Apple tutorial creates a double confusion through its naming of properties. All I do in my code is change the names, thus making clear distinctions between the view controller class (vc), the application delegate's instance of this class (_myVc), and the application delegate's getter and setter methods for getting and setting this instance (myVc, setMyVc).

C++
//
//  wndAppDelegate.h
//  wnd
//
//  Created by pi on 29/08/2010.
//

@class vc;

@interface wndAppDelegate : NSObject <UIApplicationDelegate> 
{
    UIWindow *_window;
    vc *_myVc;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) vc *myVc;

@end


//
//  wndAppDelegate.m
//  wnd
//
//  Created by pi on 29/08/2010.
//

#import "wndAppDelegate.h"
#import "vc.h"

@implementation wndAppDelegate

@synthesize window = _window;
@synthesize myVc = _myVc;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{        
    // Override point for customization after application launch.
    
    // CREATE VIEW CONTROLLER
    {
        // create an INSTANCE of the vc object/class
        // tmp is a temporary pointer pointing to it
        vc *tmp = [vc alloc];
        
        /* 
         note that vc derives from UIViewController 
         derives from bla derives from NSObject
         
         [vc.h]
         @interface vc : UIViewController <UITextFieldDelegate> 

         Cmd + double-clicking all the way through into NSobject's protocol 
         reveals retainCount -- ie it keeps track of how many things are holding 
         onto it, so that it can be garbage-collected when retainCount falls to 0 
         */
        // as we alloc it, it starts life with a retainCount of 1
        NSLog(@"vc *tmp = [vc alloc]; // [tmp retainCount] now: %d", 
              [tmp retainCount] ); // will be 1
        
        [tmp initWithNibName:@"vc" 
                      bundle:[NSBundle mainBundle]];
        
        
        /* 
         when we did '@synthesize myVc = _myVc;' XCode automatically created 
         two invisible convenience class methods: 
         ... setMyVc (setter)
         ... myVc (getter)
         that get / set the iVar _myVc
         
         NOTE: an iVar is an Instance VARiable -- (lasts only as long
         as this instance of the class we're in lasts). 
         */
        // set using setter method -- EXPLICITLY
        [self setMyVc: tmp];
        
        // ... or IMPLICITLY
        if (0.0)
            self.myVc = tmp; // executes setter: [self setMyVc: tmp];

        // Notice in the header, myVc is a RETAINed property
        // [wndAppDelegate.h] @property (nonatomic, retain) vc *myVc;
        // So, invisibly to us, setMyVc will ++myVc.retainCount to 2
        NSLog(@"[self setMyVc: tmp]; // [tmp retainCount] now: %d", 
              [tmp retainCount] ); // will be 2
        
        // but what we DON'T DO is this:
        if (NO+NO+NO*1000)
            _myVc = tmp;
            // !!! FAILs, as setting the iVar won't ++myVc.retainCount
            
        /*
         NOTE: Watch out here -- it is a common coding practice to just do 
         :
         vc* myVc; ...; property (...) vc* myVc; // in the interface, and then 
         @synthesize myVc;                      // from the implementation
         
         thus creating setMyVc and myVc setter/getter methods, and a myVc iVar
         
         Yes REALLY DUMB. a myVc getter and a myVc iVar. 
         MASSIVE CONFUSION POTENTIAL!
         
         There's 3 things:
         (1) vc* myVc;    // plain old c pointer. a simple iVar -- Instance VARiable -- 
         ................ // (lasts only as long as this instance
                          // of the class we're in lasts). 
         
         (2) -void setMyVc:(vc*) ptrToNewVcObject; // invisible setter method
         (3) -(vc*) myVc;                         // invisible getter method
         
         So don't confuse:
         (NO) myVc = bla;  which just does what it says 
                           on the can - you should NEVER need 
         ................  to do this manually - there is a SETTER method 
                           for setting it

         (YES) self.myVc = bla;   // which invokes SETTER method,
                                  // which, as myVc is a RETAINED
           or [self setMyVc:BLA]  // property, increments retainCount
                                  // and sets the iVar (myVc)
         
         LIKEWISE, dont confuse:
         (NO) vc* x = myVc;  
         (YES) vc* x = self.myVc; // or [self myVc]
         */
    
        [tmp release];
        
        /* 
         If we had just done _myVc = tmp; 
         then at this point nobody is retaining the object 
         that both myVc and tmp points to. 
         It's just going to get garbage-collected 
         at (or after) the end of the function call.
         
         myVc will be left pointing to empty space & app will crash. 
         
         But since we invoked the setter method, thus bringing myVc's retainCount
         up to 2, it will persist until we let it go by taking it back to 0 with 
         [myVc release] in the dealloc method
         */
        NSLog(@"[tmp release]; // [tmp retainCount] now: %d", 
              [tmp retainCount] ); // will be 1

    }
    
    // CREATE THE VIEW
    // If myVc.view == nil, just the act of accessing it fires myVc.viewDidLoad, 
    //   which should take responsibility for creating a view and setting 
    //   myVc.view to point to it 
    UIView* myV = self.myVc.view;
    
    [self.window addSubview:myV];
    
    [self.window makeKeyAndVisible];
    
    return YES;
}


- (void)dealloc 
{
    [self.myVc release];
    [self.window release];
    [super dealloc];
}

Where to go next?

I recommend checking out the developer videos available to you when you log into the Apple developer website. You can also find a ton of sample source code -- you shouldn't have to look too far to find it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United Kingdom United Kingdom
I like to create and invent things. Recent projects include: A novel computer interface (Google for JediPad), a new musical system (www.toneme.org), a speech typer (http://spascii.wikispaces.com/)

Currently I am making some innovative musical instruments for iPhone/iPad

PS Currently stranded in Thailand with no money! Great! So, if you like my articles, please consider putting some coins in the box. If I have to sell my MacBook and teach in a school, that means No More Articles! PayPal is sunfish7&gmail!com. Steve Jobbs -- if you're reading this, pay me to fix your documentation.

Comments and Discussions

 
Generalxcode Pin
Shining Legend20-Mar-11 1:07
Shining Legend20-Mar-11 1:07 
GeneralRe: xcode Pin
_Zorro_6-Apr-12 5:28
professional_Zorro_6-Apr-12 5:28 
GeneralIPhone development Pin
Ludek Vodicka10-Sep-10 2:18
Ludek Vodicka10-Sep-10 2:18 
GeneralRe: IPhone development Pin
Jim Crafton10-Sep-10 7:46
Jim Crafton10-Sep-10 7:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.