Advertisement

iOS

Objective-C Tutorial with xCode

Cocoa Touch | IOS | IPhone | Objective-C | Xcode

iPhone Application Development

Advertisement

This entry is part 3 of 9 in the series iPhone Application Development

Before we dive into the iPhone development, it is important that we first learn some basics of objective-c Language. So we start with basic command line tool and then make our way to iPhone development. I highly recommend you to read my previous article iPhone SDK Components & Requirements if you haven’t read yet. We will use Command line tool template to learn Objective-C Language and then we will switch to iPhone Full featured cocoa touch application.

In this Objective-C tutorial for beginners, We will use Xcode 4 to develop both command line program and iPhone application as we progress.

Getting Started with Objective-C

Step 1. Open Xcode. A window pops up asking for creating new project or open existing like below one

objective-c : xcode startup screen

 

Step 2. First option is for creating new Xcode project. Click on that one, as this is our first project in Xcode. A new window appears which ask for starting template for our new project.

objective-c : xcode project type

From Template selection window, we choose “MAC OS X => Application => Command Line Tool” and click on Next.

Step 3.  A new window appears which asks for Project Name and Type to use.

objective-c : xcode project name

 

Name whatever you call your project (I name it ‘Demo1’) and from type select Foundation from dropdown list. Click Next

Step 4. You will see the screen like below. Left bar is the project explorer, which has directory called Demo1 (project name) and also 2 more directories one is frameworks and another is products which we will discuss later on cause we rarely need those. Demo1 directory contains ‘main.m’ and Demo1.1 files. ‘main.m’ is the file we are interested in the command line tool template project. Which is contains some default code, which is import line for foundation framework and main method that will be executed when we run the project.

objective-c : xcode main work screen

 

As we can see the main method has first line @autoreleasepool code block which releases memory automatically for us and that we need in every project so that is essential and we will discuss about memory management when we switch to iPhone application because mobile device has limited memory and lots of applications are going to use it.

Next line is the:

NSLog(@”Hello, World!”);

This line tells compiler to output “Hello, World!” string on console as this is the command line tool application so we have console to output and that all we have for now. Don’t miss that @ sign before “Hello, World!” string that is Objective-C way to use string (NSString Class). @ sign tell compiler that it’s not regular c but its Objective-C. To use string in Objective-C way we have to write @”String”. NSLog function is just like printf in C Language but it accepts first argument an NSString.

Step 5. Now click on the Run button which is on top left corner of the Xcode screen

Or Go to Product => Run

Or (Command ()+ R)

And the output will be like this.

objective-c : xcode main work screen

 

That will definitely run without error for sure cause we haven’t done any code yet. Lets do some code.

Step 6. Change the main method as follow:

 

in this code first we created productPrice variable with type int and value 3000. And same way discount. Then netAmount variable that stores the deducted 10% discount from productPrice. All code is regular C except NSLog line. Let’s see this in detail.

NSLog(@”You have to pay %i to Purchase this item”,netAmount);

 

This outputs the message written in quotes but replaces %i with respective variable value after comma (,). In this case its netAmount variable so the output will be

You have to pay 2700 to Purchase this item

  • %i sign is used for integer variable.
objective-c : xcode main work screen output
So far we learn objective-c in this objective-c for beginners tutorial. Stay in touch with us by liking our FB Page for updates on iPhone Application Development. Which lead you to expert iPhone developer.
Series Navigation<< iPhone SDK Components & Requirements<< Overview of iOS PlatformiPhone SDK: Objective-C Creating Custom Class >>

Bookmark Link using: bookmark at folkd