When getting started with Objective-C and iOS development one of the first things your going to need to do is to learn how to enter code, build it and run it from within Xcode. In today’s post, we’ll take a step-by-step walk-through of how to create your first Objective-C application using Xcode.
Table of Contents
Preparation
Before you can do any real development in Objective-C and iOS you’ll need to get hold of the latest version of the development tools and you’ll probably want to register with Apple as a developer. You’ll also need to have a basic understanding on the steps that we will go through in implementing an Objective-C application. I’ve already written detailed posts on all of these so if you’ve not done so already, open a new tab in your browser and read through the steps in the following posts. Don’t worry, I’ll wait…..
- How to Register as an Apple Developer
- How to Install the Xcode Development Tools
- The Four Phases of Implementing an Objective-C Program
Right. All set? Lets get on with creating our first Objective-C application.
Create Your First Objective-C Application
Head over to your applications folder and start Xcode. Once loaded Xcode will show you a Welcome Screen similar to the picture below:
To create a new Objective-C project select Create a New Xcode Project from the Welcome Screen. As an alternative you can select File > New > New Project… from the menu at the top of your screen.
In the Mac OS X section left-hand pane select Application. In the upper-right pane then select Command-Line Tool followed by Next.
Enter HelloiOSDevTutorials as the Product Name.
Enter your name for the Organization Name. For the Company Identifier, enter something that should be globally unique if you can. The convention is to enter your websites address in reverse (e.g. com.iOSDevelopmentTutorials). Don’t worry if you haven’t got a web address of your own. Just use mine for now.
Make sure the project Type is set to Foundation and make sure the check box for Use Automatic Reference Counting is selected. Once your done, select Next.
On the next screen browse to where you want to store the new application. I created a new folder within my home directory called Projects that I’ll use for all my Xcode projects.
Leave the Create local git repository for this project check box selected and then select Create.
Xcode will now run-around in the background and create a load of files that make up your new project. Once done Xcode will display its Xcode Workspace Window and a scary list of options for your project. The key now is not to panic. I know it looks pretty daunting but you can ignore all of this for now.
Editing Source Code
If you haven’t done so already, make sure you’ve got the main.m
file selected in the Navigator Area. The Xcode Edit Area should update to display the contents of the file. We’re not going to go through the contents of the file just now (it’s the subject or our next post), but use the Edit Area to look for the following line:
NSLog(@"Hello, World!");
and then change it to:
NSLog(@"Hello, iOS Development Tutorials!")
Congratulations, you’ve edited your first Objective-C source code! Now lets get setup for building and running it.
Displaying the Output Window
Before we go through Xcode’s build and run phases, we first need to configure Xcode so we can see can actually see the output from our application.
The easiest way to do this is by selecting the middle button in the View section in the Xcode Toolbar. This can be found in top-right of the Xcode Project Window and toggles the display of the Debug Area (Alternatively you can select ⇧⌘Y on the keyboard which has the same effect).
When we run our application its output is going to appear in the right hand panel of this Debug Area so keep an eye out for it. Talking of which, lets get on and do that now.
Building and Running Your Application
At the left-hand side of the Xcode Toolbar you should see a stereotypical play button. This is Xcode’s Run button and selecting this will instruct Xcode to take the current source code and pass it through the build and run phases we discussed in our other post.
Press the Run button now and see what happens.
If everything went well you should see your program’s output in the right-hand panel of the Debug Area.
Yeay! Hello iOS Development Tutorials in all it’s glory and your first successful Objective-C Program. We’re making progress!
But what if things went wrong and you didn’t get any output?
Warnings and Errors
If there is some sort of mistakes in your program Xcode will tell you about it. There are two types of mistakes that Xcode will notify you of:
- Fatal Errors
- Warnings.
Fatal errors are indicated by a red stop sign with an exclamation mark.
These are errors that must be fixed in order to run your program. They normally indicate that something is badly wrong in the code that you have written. I’m afraid you have no choice. You have to fix these issues before you’re program will run.
The second type of error are warnings. Warnings, as you might expect, are less severe than fatal errors and are denoted by a yellow triangle containing an exclamation mark.
Warnings won’t prevent your program from running but can lead to some unexpected side-effects and your program not working as you would have expected.
My recommendation is that you therefore treat warnings in the same fashion as fatal errors and fix all of them before you run your program.
Diagnosing Issues
So Xcode has told you you’ve got an issue with your source code. How do you diagnose what the problem is?
One of the easiest ways of seeing all the issues in your program is by selecting either the warning or error symbols in the message panel at the top of the Xcode Project Window. This will display the Issues Navigator in the Xcode Navigator Area.
The Issues Navigator provides you with a central place to see all the feedback messages from the Compiler and Linker.
As your experience grows these messages will help you diagnose what is wrong with your program. Sometimes these messages are somewhat cryptic but over the course of this blog I’m hoping that you will grow to understand what these tools are trying to tell you.
If you select any of the errors or warnings in the Issue Navigator, the Edit Area will update and highlight the line that is causing the problem along with the error or warning message. By doing this, Xcode helps you quickly narrow down the line you need to focus on to work out what is wrong.
As we’re just getting started, we’ll ignore these messages for now (we don’t really have enough knowledge yet to know what they are telling us). Instead, if you do have warnings or errors, carefully compare your code line by line to the code below and fix any mistakes. Once you’re done try to re-run it by selecting the Run button. Hopefully, if you copy it carefully you should have some success. If you don’t, leave a message at the bottom of this post and I’ll try and help you find out what went wrong.
//
// main.m
// HelloiOSDevelopmentTutorials
//
// Created by Andy Bargh on 30/03/2013.
// Copyright (c) 2013 Andy Bargh. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, iOS Development Tutorials!");
}
return 0;
}
Summary
In today’s post we took our first steps down the path to learning Objective-C. We learnt about the major stages that you have to go through to turn your source code into a running application and then we spent the rest of the post creating, editing, building and running our first Objective-C application. In the next post we’ll build on this work and take a step-by-step walk-through of our main.m
file and really get an understanding of what actually does. I hope you’ll join me.
Image source: https://unsplash.com/photos/zMcBf-n6qrs