Xcode Primer/Tutorial/How-to

A Google search for Xcode examples and how-to’s etc.. returns a lot of results, however after following a few steps it becomes clear that the older tutorials simply can’t be followed. The newer Xcode (3.2.1 is current) has had it’s UI changed so much, especially the interface builder and the majority of instructions no longer apply.

I’m writing up the steps i’m following to learn, so that I can follow them when I have forgotten something (which I frequently do) and so that anyone else can follow them if they wish. I don’t intend to ever go too far with Xcode development, so don’t expect to find a how-to to developing a game or a photoshop alternative.

Introduction

Before we start writing any code it’s probably best to go over some of the basics of Xcode and in particular Objective-C that is used to build cocoa apps and iPhone apps. It’s possible to develop your application without using Xcode and compiling with the gcc compiler but thats well beyond what I have the inclination to do and the Xcode UI comes with numerous benefits (along with some problems).

Xcode

Xcode consists of numerous peices:

– Xcode.app: the IDE used for coding, and a wrapper for the debugger and compiler for your applications.

– Interface Builder.app: The UI used to craft the GUI for your application and tie the actions of GUI to your code and receive it’s outcomes.

– Developer Tools Library: This has to be subscribed to and downloaded(free), and is the source code documentation.

The Xcode.app, works pretty well and will attempt to autocomplete your function names or class names, and points out build errors pretty well. The Interface Builder.app on the other hand is terribly buggy and will often leave you frustrated it can lose class information leaving you without the correct actions when you try and link your GUI to the code. The other problem that is common is it’s lack of getting focus when switching back from another application. If you’re in safari reading something and switch back to the Interface builder you’ll be interacting with it, however the app in focus is still safari meaning functionality doesn’t work (minimize safari and then try using Interface builder).

Objective-C

I’m not going to go into too much detail here as this isn’t a University training course, and if you’ve got no real programming experience (Java/C/C++) then you’ll possibly struggle here. I’d suggest getting your head around the basics of C++ before working with Objective-C as there is overlap, it’s just that the syntax is noticeably different.

Declaring a class in objective-C

An Objective-C class depends on two pieces, which are the ‘interface’ and the ‘implementation’.

The interface consists of the declaration or ‘design’ of a class, ranging from:

– it’s name

– classes it inherits from

– and it’s instance variables and methods that it has.
[code lang=”c”]
@interface DeclaredClassName : ParentClassToInheritFrom
{

int    ClassVariable;   // Variables for Class being declared

}
-(id)ClassMethod:(NSString*)aString;

@end
[/code]
The implementation the consists of ‘fleshing out’ the class, such as providing the code for the methods declared in the interface.

[code lang=”c”]
@implementation DeclaredClassName
-(id)ClassMethod:(NSString*)aString
{

NSLog(aString);   //place the string in the system log

}
@end
[/code]

Linking it all together

The next step is linking together a GUI to your classes and enabling your buttons and various other elements access to the methods and variables declared in the code. This sadly has to be done using the Interface Builder and can lead to a bit of confusion as ‘bugs’ can appear, however the concept is as follows.

1. Open your default interface created by the project builder in Xcode (default it will be MainMenu.xib)

2. Numerous windows will be open, however the one to focus on is the main project window containing the elements that make up the GUI not the GUI designer itself (more detail and screenshots will be shown in the next post)

3. From the library you’ll want to drag the parent class to this window (usually we will inherit from NSObject), and then from the inspector window we can find the identity tab, and change the class to the ours (DeclaredClassName, from our example).

4. GUI elements can now use the methods that are declared in this class, so for example a button would have access to the method ClassMethod.

Next Step will have to be hello world.

One thought on “Xcode Primer/Tutorial/How-to”

Leave a Reply

Your email address will not be published. Required fields are marked *