Learn Objective-C: Day 1

Learn Objective-C: Day 1

Tutorial Details
  • Technology: Objective-C
  • Difficulty: Beginner
  • Completion Time: 30 - 60 Minutes
This entry is part 1 of 6 in the series Learn Objective-C

Welcome to my series on coming to grips with the awesome language that is Objective-C. Throughout this small series of articles, my aim is to take you from no prior experience with Objective-C to using it confidently in your own applications. This isnʼt a rush job – so donʼt expect to just skim through the basics and be away – weʼll be going through not just the bare essentials, but also the best practices you can apply to ensure your code is the best it can be. Letʼs jump straight in!

What is Objective-C?

If youʼre reading this series then Iʼll hazard a guess that you already know, but for those of you who donʼt, donʼt worry as by the end of this part youʼll know what it is back-to-front and inside-out.

Objective-C is an object oriented language which lies on top of the C language (but I bet you guessed that part!). Itʼs primary use in modern computing is on Mac OS X as a desktop language and also on iPhone OS (or as it is now called: iOS). It was originally the main language for NeXTSTEP OS, also known as the operating system Apple bought and descended Mac OS X from, which explains why its primary home today lies on Appleʼs operating systems.

Because Objective-C is a strict superset of C, we are free to use C in an Objective-C file and it will compile fine. Because any compiler of Objective-C will also compile any straight C code passed into it, we have all the power of C along with the power of objects provided by Objective-C.

If youʼre a little confused at this point, think of it this way: everything C can do, Objective-C can do too, but not the other way around.

What will I need?

Throughout this series, we will not focus on building applications for the iPhone. Instead, we will concentrate more on the language itself and for this reason all you will need is a Mac with a compiler such as GCC. If youʼve installed the developer tools from Apple (Xcode, Interface Builder, etc), then GCC should already be installed. If not, then head to Appleʼs developer website and get yourself a free copy.

As far as prerequisites go, while I donʼt expect you to have a full background in computer science, some knowledge of programming in general or of C in particular would definitely be a bonus. If you donʼt have much prior programming experience, donʼt worry -youʼll pick it up in no time!

If youʼre running Windows (which is unlikely as this tutorial is aimed at iPhone developers) you can still compile Objective-C on your system using a compiler such as CygWin or MinGW. This tutorial series is catered to Mac users, but if you are using Windows and encounter any problems then be sure to leave a comment and Iʼll see if I can help.

Compiling your code

Before you can see your code in action, you need to be able to compile it into something executable. Hopefully you now have your copy of GCC ready. Compiling is really easy, a simple one line command.

NOTE:
Compiling is the process of “translating” a high-level computer language, like Objective-C or PHP, into a low-level machine code that can be processed by a computer when the program is executed.

All the programs that we see running in our fancy Mac OS operating system consist of a series of instructions that are visually displayed to us in a GUI, or Graphical User Interface. In contrast to the GUI program interaction with a mouse that most of us are familiar with, it is possible to issue commands directly to the operating system through a text-based interface known as a “terminal” or “command line.”

The command line application in Mac OS is called Terminal and can be found in Applications -> Utilities. Go ahead and open Terminal now (you can also search for it in Spotlight). Terminal has several basic commands you should be aware of in order to properly utilize it. One of the most important commands to know is cd, which stands for “change directory.” This command allows us to change where in the filesystem Terminal is reading from. We canʼt just tell Terminal to compile our file if we donʼt show it where the file is first! To switch to a desired directory, you can use a full path such as:

cd /Users/MyName/Desktop/Test

You can also use relative paths, allowing you to only type a single folder name in some cases. For example, if youʼre already in your Desktop folder, you could simply type:

cd Test

to get to the Test folder.

What if you want to see where you currently are? The immediate folder name is displayed before the prompt (the bit where you type). For example, if your prompt says Dan- Walkers-MacBook: Desktop iDemonix$ I can assume Iʼm in the Desktop folder. If you aren’t sure, you can also type pwd to display the absolute filepath of the current location.

If you want to list what files and folders are in the current folder, use the list command: ls. Finally, if you wish to go up a directory to a parent folder, type “cd .. “. So, if we were in the Test folder, which is inside the Desktop folder, but we wanted to go to the Desktop folder instead, we could type cd .. to go up to the parent directory, Desktop. If we wanted to get to the home directory we would type cd ../.. to go up two levels. Alternatively, to get to the home directory you can just type cd ~ from anywhere.

When using the Terminal application, compiling looks like this:

gcc inputfile.m -o outputfile

Youʼve probably already guessed how it works: inputfile.m contains our code (.m is the extension used for Objective-C files) and -o tells gcc we want our executable to be called whatever we specify next, which in the example above is outputfile. To run our creation after compiling, we simply type:

./outputfile

Simple.

When you compile, the compiler will generate any errors, notifications or warnings related to the syntax of your code. Errors generated when compiling are understandably referred to as “compile-time” errors, and this is often the most stressful part of writing an application (especially when your code isnʼt compiling because you put a single character in the wrong place or forgot to end a line with a semi-colon). Compiling can also take time when youʼre writing large applications consisting of multiple files, which is also another reason why compiling can be a tedious experience. This fact has led to a ubiquitous programmer joke often seen on the t-shirts of men with unkempt beards: “I’m not slacking off. My code is compiling.”

The Basics

Objective-C itself isnʼt that hard to learn. Once you get to grips with the basic principles, you can pick the rest up as you go along pretty easily. You do need to have an understanding of the fundamentals of C programming though, and that is what the rest of this tutorial will cover.

Letʼs look at a basic application in C:

#include <stdio.h>
int main(){
    printf("Hello World\n");
    return 0;
}

All this application will do when you run it is display the string “Hello World” in Terminal and exit.

NOTE:
Curious about the “return 0″ statement? Because we told the compiler that the function main() will return an integer, we return the constant integer value ’0′ at the end of the function. By convention, returning ’0′ signals to the calling program that our program finished execution without any errors.

To try this for yourself, fire up Xcode and make a new Objective-C class. Delete all the code Xcode gives you by default and stick the above code in. Once youʼve done that, you can compile it using Terminal. Open Terminal and change to the location where your file is, if you saved to the desktop then simply type cd desktop so that Terminal is now reading from your Desktop. Then type this command:

gcc program1.m -o program1

Your program should compile with no errors. To run it, simply type:

./program1

Then hit return.

Awesome, so what actually happened there? Well, first we imported a library called stdio which manages the standard i/o (input output) functions, like printf(). We then create a function called main which should return an int or integer which is basically a number with no decimal point. We then use the printf() function to output ʻHello Worldʼ in to terminal. The \n we use tells Terminal to put a newline after the text. Finally, we return 0 (remember we said main should return an integer) which tells the operating system everything went fine. We use the name main because this is triggered automatically when the program is executed.

So far everything should be pretty simple: we wanted to write some text to Terminal, so we imported a library with a function for writing text, then we used a function from that library to write the text. Imagine that what you import is a physical library and printf() is one of the books available.

Variables

Soldiering ahead, weʼre now on to variables. One of the fundamental things we need to be able to do in our applications is temporarily store data. We do this using variables, which are containers that can hold various types of data and be manipulated in various ways. We use variables to store all sorts of data, but we must first tell the compiler what weʼre going to store in it. Here are several of the most important variables that you should know about for now:

  • int – for storing integers (numbers with no decimal point)
  • char – for storing a character
  • float – for storing numbers with decimal points
  • double – same as a float but double the accuracy

When weʼre not using variables, weʼre often using constants. A constant will never change: we always know what the value will be. If we combine constants we get a constant expression, which we will always know the result of. For example:

123 + 2 = 125

This is a constant expression, 123 + 2 will always equal 125, no matter what. If we substituted a constant for a variable, the new expression would look like this:

123 + i = ?

Because i is a dynamic variable, we do not definitely know the result of this equation. We can change i to whatever we want and get a different result. This should give you an idea of how variables work.

One thing we still need to know is how do we display variables like we displayed “Hello World” above? We still use the printf() function, except it changes a little this time:

#include <stdio.h>
int main(){
    int someNumber = 123;
    printf("My number is %i \n", someNumber);
    return 0;
}

What weʼve done here is told the function printf() where we want our integer to appear, then where it can be found. This is different to a lot of languages such as PHP where you could just place the variable in the text.

We are not just limited to just one variable in printf(). The function can accept multiple parameters separated by commas, so we can pass in as many as we have formatting signs for in the text. Above we use %i as a formatting sign because we were including an integer. Other variables have their own format specifiers:

  • %i – integer
  • %f – float
  • %e – double
  • %c – char

One thing I want to touch on before we move on is the char type. A variable of type char can only handle single characters, when thatʼs all we need this is great, but if we need a string of text itʼs pretty useless. To get round this, we use something called a character array.

Imagine you have a sentence that is 11 characters long (like ʻHello Worldʼ – donʼt forget to include the space), a character array is like having 11 charʼs but all glued together. This means that the value of the character array overall is ʻHello Worldʼ but char[0] is ʻHʼ. In brackets is the char youʼre after, because we put 0 we get the first character. Donʼt forget that counting in arrays usually starts from 0, not 1.

Conditionals

When an application needs to make a decision, we use a conditional. Without conditionals, every time you ran your application it would be exactly the same, like watching a movie. By making decisions based on variables, input or anything else, we can make the application change – this could be as simple as a user entering a serial number or pressing a button more than 10 times.

There are a few different types of conditionals, but for now weʼre just going to look at the most common and basic: the if statement. An if statement does what it sounds like, it checks to see if something is true, then acts either way. For example:

#include <stdio.h>
int main()
{
    if(1 == 1) { // This is always true
        // Do some stuff here
    }
    return 0;
}

If 1 is equal to 1, then whatever is between the brackets is executed. You might also be wondering why we used two equals signs instead of one. Using two equal signs is an equality operator, which checks to see if the two are equal to each other. If we use a single equal sign then weʼre trying to assign the first value to the second value.

Above, since 1 will always be the same as 1, whatever is in the brackets would be executed. What if we wanted to do something if this wasnʼt true though? Thatʼs where else comes in. By using else we can run code when the if conditional returns false, like so:

int main(){
    if(1==1){
        // Do some stuff here.
    }
    else{
        // The universe is broken!
    }
    return 0;
}

Of course, in real life, we wouldn’t be checking to make sure 1 is the same as 1, but the point is made. Consider an application that closes if you press the close button three times (annoying but relevant). You could check in the brackets to see how many times it has been pushed. If it is lower than 3, then your else block could execute code to tell the user how many more times the button must be pushed to exit.

Weʼll look at conditionals more when we come to use them in our applications further along in the series.

Loops

Now let’s investigate a programming loop. Loops, as the name suggests, let us loop through a piece of code and execute it multiple times. This can come in very handy in situations such as populating a list or repeating a piece of code until a conditional returns true.

There are three types of loops, in order of most common: for, while, and do. Each one is used to repeat execution of a block of code, but they function differently. Here are examples of each:

// if loop
int main () {
    int i = 9;
    int x = 0;
    for (x = 0; x < i; x++){
        printf("Count is: %i\n", x);
    }
    return 0;
}

This may look a little complex at first, but it really isnʼt. In the parentheses after for is the initiator, a conditional, and the action. When the for loop starts it executes the initiator, which in our case above sets x to 0. Each time the loop runs (including the very first time) it checks the conditional, which is "is x smaller than i?" Finally, after each loop through the code, the loop runs the action - which above increments x by one. Simple. Since x is increasing by one each time, x will soon no longer be less than i and the loop will finish and the program will carry on running.

// while loop
int main () {
    int x = 0;
    while (x < 10){
        printf("Count is: %i\n", x); //Watch OUT! Something is missing.
    }
    return 0;
}

Similar to the for loop, the while loop will execute the code between the brackets until the conditional is false. Since x is 0 and we don't change it in the code block, the above would run forever, creating an "infinite loop." If you wish to increment x, then in the case of our while loop you would do this between the brackets:

// while loop
int main () {
    int x = 0;
    while (x < 10){
        x++;
        printf("Count is: %i\n", x);
    }
    return 0;
}

The do loop is essentially the while loop, except the conditional runs after the block of code. What this means is when using a do loop, the code is guaranteed to run at least once:

// do loop
int main () {
    int x = 0;
    do {
        x++;
        printf("Count is: %i\n", x);
    } while(x < 10);
    return 0;
}

Pointers

Pointers can cause a lot of confusion with newcomers to programming or just newcomers to C. It is also not immediately clear to some people how they are useful, but youʼll gradually learn this over time. So, what is a pointer?

As the name implies, pointers point to a location. Specifically, locations in computer memory. Think of it like this, when we create a variable (let's say it's an integer called ʻfooʼ as is so popular with programming theory) and give it a value of, for example 123, we have just that - a variable with a value of 123. Now, if we setup a pointer to foo, then we have a way of indirectly accessing it. That is, we have a pointer of type int that points to foo which holds the value '123.' This would be done in code like so:

    int foo = 123; // This is an integer variable
    int *ptr = &foo; // This is a pointer to an integer variable

Clear as mud? Donʼt sweat it. Pointers are hard - often considered the hardest thing to learn when picking up the C language. Pointers will eventually become second nature to you though, and there will be more on pointers within Objective-C further in this series.

Wrapping Up

You've just been given a crash-course overview of the C language fundamentals. This part of the series was intended to be a quick primer on C to get you ready and prepared for the rest of the series, and should have been especially useful to those who are already familiar with programming in another language. If you are new to programming in general or are still in doubt about any basic of C, re-read the above and feel free to leave questions in the comments.

Before next time, be sure to try and compile your own programs using the code above. Set yourself small challenges, such as making a loop execute 10 times and count each time through the loop using printf. Thereʼs no harm in trying and experimenting, if it goes wrong then itʼs probably even better as itʼll get you on the right track to troubleshooting your own code.

Challenge

For this week, we will end on a simple challenge. You are to create three programs that count to 10 using each type of loop. Since we will use loops often in Objective-C, itʼs good that you learn to create them by heart. That should be pretty easy, so try to count down from 10 to 1 afterwards (if ++ increments by one, what could be the code to decrement by 1?).

Next Time

In the next installment of this series, Iʼll provide an overview of how Objective-C works. Weʼll also look at object orientated programming and its uses, as well as really drill down into classes, instances, methods, inheritance and more.

Next week should really help you to understand what makes Objective-C such a great language and why it really extends the C language in so many useful ways.

Any Questions

If you have any questions, you can either leave a comment below where Iʼll try and keep checking or you can shoot me a message on Twitter (http://www.twitter.com/iDemonix) where Iʼll get back to you ASAP.

Series NavigationLearn Objective-C: Day 2»

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Chris

    Ha I’m only 14 and have no development skills would you recommend this tutorial for me, if not what do I do?

    • Shawn

      I am in the same boat as you chris, I am 15 and I am just learning how to code/create applications for the IOS. What my personal experience has been is, I did the Learn Objective-C classes first, then moved onto Beginning iOS Development video tutorials. It seemed to run really smoothley, and it was better to learn the language before I jumped into that. I hope this helped you, as these fine gentelmen have helped me. Best of luck with the learning process.

      Shawn

      • Murray

        Same here, I have used visual basic at school and am looking into Objective C and this so far is great

        Thanks, I’ll be working through the tutorials

    • http://www.danwalker.com/ Dan Walker
      Author

      Hey Chris,

      I started programming at the geeky young age of 12, age really isn’t a factor with things like this. If anything, the younger you start learning the better, that way by the time you come to employment age or to a stage where you want to go full time freelance, you’ll have a lot more skills than most.

      Good luck!

    • Jonny

      Well Chris, I’m 12 and have basic programming skills. If you’re using a windows, forget this. This is solely for Apple products. Me, as I have a mac, will use this all of the time. If you’re looking to be a developer, a good thing to do is learn HTML first. It’ll get you into the habits of programming, especially Javascript. When you’re ready to move on, I’d suggest applescript for mac, and when you’re finished with that or you have windows, get Python. It’s fairly easy and straightforward. Otherwise C++, it’s very powerful and definitely easy. The only reason I say Python first is the simplicity. I could build me a good text program with user input within 10 minutes of reading the documentation. C++ is my first real committed language. It’s easier than C, but I’d learn C after Python if you’re on a mac, because after you learn C++ as I am, learning C is like learning C++ all over again but more complicated. Start with C, then go into objective-C. This is wise as Obj-C as I call it is used for the GUIs in OSX and the IOS. As I said before, if you’re not targeting Macintosh machines or IOS devices, learn C++ first, as it’s easier, just as powerful, and cross-platform. Once you get that far, I’d recommend Java, as it seems like a great language. Note that I’m only in the stage where I know C++ completely, and am learning Obj-C. These are really my opinions. Also note that I’ve been “Programming” (If you consider HTML a language) since I was like 9.

      • Mighty

        @Jonny, this aint solely for apple products :-) I’m using Ubuntu and able to compile all the programs on this page. Now I look forward to exploring the other tutorials.

        @iDemonix, many thanks for this awesome series. Just trying to take a new drive to iOS. Best wishes :-)

      • Curious Onlooker

        Sorry, but I don’t think any programmer would ever call HTML actual programming.
        The ‘L’ in the naming acronym is somewhat misleading, as it’s not really a language, and the closest thing to programming you could learn from it is that opening tags must have closing tags.
        It’s a method of displaying and formatting information, nothing more.

        Coming from the old-fashioned school, nothing that is interpreted at run-time constitutes programming.
        Programming is formulating a set of instructions, that are in turn read by a compiler (not pre-compiler), and are then turned into binary code.

        You *can* then start to add Javascript to HTML, and you will begin to approach programming methodology, but it is still interpreted.

        Even the PHP I sometimes use today is not real programming (although I m sure there are many who would argue otherwise), as again, it’s interpreted. I like PHP’s simplicity and flexibility, none-the-less, especially when combined with AJAX.

        I admit to giving up on ASM, but went through the C/C++ line of schooling, starting when DOS meant Disk Operating System, not Denial Of Service, and Windows programmers were a rare breed.

  • BoSSa

    THNX alot!

  • maya

    “copy to clipboard” does not work.. code does NOT get put in yr clipboard.. (and I’ve tried more than once..)

    (why do all tutorials publish code with line nos.? without line numbers we could copy code right from the page, instead of having to open a new window to see the plain code.. this is not too practical..)

    thanks for great tutorial, though..

    • Charles

      If you’re like me, you’ll never learn unless you type it out anyway ;)

      • http://www.danwalker.com/ Dan Walker
        Author

        Fantastic point right there!

  • Amadou

    I went through day one and this is easily the best walkthrough I have read yet. This coming from a highschooler with only programming knowledge of two terms of java. Great job and I recommend this tutorial.

  • BR

    Hi,

    I just wanted to say thanks for putting out a tutorial for Objective-C. I have no experience at all with computer code but I’ve always wanted to learn. This is a really, really basic question (which will quickly reveal my inexperience), but for some reason in tutorial 1 I can’t even get the “Hello World” to appear in terminal.

    First, I guess I wanted to make sure I was even opening a new Objective-C project and inputting the data in the correct place. Could someone tell me specifically what I need to do to make sure I’m in the correct place(this is with Xcode 4.0.2). For example, I open it and don’t know what template I need to choose for a new project (e.g., Cocoa Application, Command Line Tool, etc.). After that, what would I need to do?

    (Right now I’m going to: Coca Application under Mac OS X application, typing in the product name, then saving it to the desktop. Next, I’m opening a new file, going to Cocoa under Mac OS X and then selecting Objective-C class. Then I deleted all the code written once opened and tried inputting the stuff mentioned for “Hello World”)

    If I am in the correct place, I was wondering why the code isn’t showing up. When I try to go into terminal and type in ‘gcc program1.m -o program1′ it reads:

    i686-apple-darwin10-gcc-4.2.1: program1.m: No such file or directory
    i686-apple-darwin10-gcc-4.2.1: no input files

    So… I’m not really sure what’s going on, and, again, I’m a super, super noob at this. Tonight was my first night ever even trying code. Thank you so much for any suggestions/advice you can give me!

    • TB

      @BR I am also very new so I hope I tell you correctly. ded up starting an “Empty” project under the “Other” section. That gave me a header(.h) file and the Obj-C file (.m). I dropped in the above code and saved it. I didn’t save mine on the desktop so I right-clicked on the .m file and hit “Show in Finder”. I then went to the Terminal and typed “gcc”, back to the Finder where my file was still highlighted and dragged the file into the Terminal. That will enter the absolute path of your file in the Terminal. You can then add the -o and the program name and go from there.

    • Brandon

      You’re going to want to open up Xcode, go up to File>New.New File (Hotkeys CMD+N). Choose Objective-C class. You’ll be able to write out your code there for tho tutorial. After inputting your code, save it to the desktop. Go into Terminal, type cd desktop Now type gcc nameofyourfil.m -o nameofyourfile You have to make sure you’re in the right directory in Terminal. If you’re in the wrong directory, it’s pointing at the right file, but in the wrong folder. Good luck!

    • stone_1

      @BR In Xcode go to Preferences>Downloads and install ‘Command Line Tools’

  • http://Vgggg Nick

    I don’t get the loop thing

    • Luan

      ill explain a loop lets say the loop will continue while x is smaller then 10.
      know whenever x is smaller then ten the action will redo itself.
      this is coming from a 12 year old kid with a knowledge of c# and java

  • ken

    hi
    when i want to compile the file”gcc inputfile.m -o outputfile”
    it said bash: gcc: command not found
    how can i fix it???

    i am using windows xp and CygWin

    like this image
    http://i1128.photobucket.com/albums/m497/fail50/OBJECTIVEC.jpg

    thank you!!!!!!!!

    • http://google.com sc8ing

      This tutorial is for Mac, as it is using XCode. You would have to download and install the gcc program for windows before you would be able to compile it using that method. With that said, I have absolutely no idea whether or not there is one, anyways.

      My suggestion: get a Mac ;)

    • Ian

      Cygwin allows you to run linux apps on windows through a linux shell basically. I’m starting this tutorial on Ubuntu linux because as far as writing c and compiling with gcc it’s the same as on the unix based Mac.

      The package is gcc and you can install it through cygwin. I haven’t messed with cygwin for a while so my memory is cloudy but there is a package manager that runs when you run the cygwin installer. You can install new packages by re-running the installer. You can browse through the packages and look for gcc, gcc-4.4-base, and gobjc-4.4 and gobjc-4.4-multilib. At least that’s the packages for Ubuntu Linux. If they are in the package repository you can install them and should be able to follow along using cygwin at least as far as basic objective-c development.

      Of course this is new to me so I dont’ know if it will come to a point that you and I won’t be able to continue without a mac but for the hello world and other simple examples you can compile and run them the same way. We’ll see I guess :-)

      Hope that helps some.

      • Ian

        Ah, it’s hopeless anyway I think. While we can write objective-c all day on Linux, cygwin, or windows with ming. I don’t think there will be a way to get the ios sdk installed on either to make ios apps with.

    • Manu Singh

      This is problem with ur compiler path. Check compiler path is exported or not. Even u can check PATH in cmd prompt or in Environment Variable-> PATH.

  • Ben

    Im dying to get started with this tutorial but i cannot figure out how to get Xcode to work on my computer. I downloaded Xcode 3.2.6 on my mac and everything went well until i tried to open the program and it simply said “Xcode cannot be opened because of a problem.” I am using OS X 10.6.6 and it should be compatible.I don’t think i need to dl Xcode 4.
    What can i do to get this working correctly so that i can begin.
    Also I tried moving ahead and putting some code into terminal but it is impossible to organize my code without being able to use multiple lines. I see in your tutorial you have numbered lines. Is that supposed to be what my terminal looks like?

  • Hiba Osama

    Hello , i just want to say that your lessons are very good , am not a new comer to programing , i started programing by java so C is not difficult for me and your lessons was a very good intoduction to this language. but my question is why do we use pointers? you said to point to a locations of variables in memory…! i understand that very much but why and for what reason we use it?

    thankx alot.
    Kind Reqards,

    Eng. Hiba Osama M. Zaki
    Email : hiba.osama@gmail.com

  • Fama

    Xcode is no longer free so its very hard to learn this without a program. Any alternatives?

    • Mathias

      Since the release of osx Lion xcode is available for free through the appstore.

  • dan

    PhP compiles??

    very good tutorial so far, I have about 8 years dev expereice and a software engineering degree so i should find it easy.

    To the young guys: Go for it, i started playing around with python and C when I was around your age — its a bit of a steep learning curve but remain persistent and you will pick it all up on no time.

  • http://www.berdjoangyuk1111988643w8.com Madeleine Eckstrom

    I’ve been browsing online more than three hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. Personally, if all website owners and bloggers made good content as you did, the web will be much more useful than ever before.

  • Vishal

    Really sir…. U r great….

  • JB

    Tutorials seems very elaberating and descriptive which is very useful for begginers..
    i just started with Day-1.Hope rest are good too.

    Thanks sir..

    regards,
    JB

  • rahul

    how to run objective c code in windows xp??

    i already download MinGW nw what should be the next step plzz help meeeeeee…..

    • AgentG

      Rahul, You need an Intel Mac with Snow Leopard or greater to run Xcode, which is the Apple environment for Objective-C applications (both iOS and Mac OS). Otherwise, it doesn’t make much sense, because you do not have the Cocoa framework and you cannot compile for Mac. AgentG

  • Marco

    Hi i have one question regarding incrementing the variables. if i want an increment bigger than one what should i use instead of eg. “X++” ?

    Thanks in advance for your response.

    • http://mobile.tutsplus.com Mark Hammonds

      The most basic syntax is: x = x + 10; This would increment the X variable by 10 instead of one. Alternatively, you could use the shorthand: x += 10; to achieve the same.

  • http://www.constructionmanagementapps.com James

    I downloaded Xcode 4.2 for Snow Leopard and then I tried to follow your details in The Basics as follows:

    ” fire up Xcode and make a new Objective-C class. Delete all the code Xcode gives you by default and stick the above code in. ”

    I was immediately lost for this. I opened Xcode, I had to set up a project and the screen was very complicated. I was not sure of anything for this. Please give specific steps for this.

    I was able to open Terminal immediately but nothing I tried worked. All other discussions seemed simple enough but, I was never at the correct starting points.

    Thanks

  • Chris Allen

    hi, i was wondering if you had any suggestions for me as well, i have a few app ideas i would like to put into the app store, ive even went as far as drawing and making a complete outline for all of them… all is left is to learn the actually code to do it, ive never programmed but ever since ive started looking into objective c a few months ago im starting to like it more and more but still feel really lost and confused where as i still cant manage to just write a basic program… where should i start, is this site really the right site for me??

  • Abduullah

    hi,

    I learned before that if we want to print the integer number, we use %d not %i. So why here we use %i ?

    thanks,

    • http://www.danwalker.com/ Dan

      Hi, %i is for an integer and %d is for a double :)

  • http://boneville.com vi

    Dan – thanks so much for this tutorial. I’ve been out of programming for 20 years and now feel the need to have a basic understanding of app programming. This has been v. helpful and straightforward so far.
    vi

  • William

    I’m starting with my coding, I know some basic Java, but when I try to compile my code in Terminal, it gives me this error:

    AppDelegate.m: In function ‘tests’:
    AppDelegate.m:7: warning: incompatible implicit declaration of built-in function ‘printf’
    Undefined symbols for architecture x86_64:
    “_main”, referenced from:
    start in crt1.10.6.o
    ld: symbol(s) not found for architecture x86_64
    collect2: ld returned 1 exit status

    I’m using Xcode (of course) and I tried importing stdio.h but it still gives me the same message. The app runs in Xcode, just not in Terminal. What can I do to make it work? (This happens for all of my programs)

    • FI

      Hi,
      I added #include in the line above the code related to looping which got rid of the warning: incompatible implicit declaration of built-in function ‘printf.’ It worked perfectly after that
      Fi

      • Fi

        Sorry, correction
        i added: #include in the first line before the looping code

  • Calley

    I’m 10 years old and just finished learning C/C++ and Java. Thanks for this tutorial since it’s not offered in my prep school.

    • http://www.danwalker.com/ Dan

      Hi Calley,

      I started from a young age too, it’s the best way, stay motivated!

  • http://umumble.com/blogs/macosxdev/89/ Ermoks

    Also, if you want to learn Objective C you can read this article Objective-C from a scratch

    I think it will help you to improve your knowledges

  • Ronny

    Hey guys, I’m only three and I just finished developing my first iPhone app. So don’t worry if you’re only 12! You will easily master it!

    • Rolly Polly Lover!

      Yah right your 3. 3 year olds can’t type (Without their parent’s permission)or memorize it all. Peeps: reply if you agree with me! ;)

      • Joe

        *crickets*

  • Aubrey Todd

    If you would like to use Xcode instead of the Terminal, do this:
    1) Open Xcode and select ‘Create a new Xcode project’,
    2) In the next window select ‘Command Line Tool’ and select Next,
    3) Give your project a name and change the Type to ‘C’ and select Next,
    4) Save your project in the desired folder,
    5) The next window should show you a list of files/folders on the left side, select ‘main.c’,
    6) You will see a list of program steps Xcode wrote for you, #include is already there
    and ‘int main (int argc, const char * argv[]) { // insert code here…}’ is there. Do not erase
    this stuff. The code in () following ‘main’ is required for the program to run in Xcode.
    Where you see ‘ // insert code here…’ you can put in what you want. The ‘Hello World’ that
    is there will compile and run. The result is shown in the Console which should appear at the
    bottom of your screen. If it doesn’t, in Xcode 4.2.1 there is a view selection in the upper
    right part of the screen to show it.
    Some of you were having a problem with Terminal, I hope this helps as it avoids using the Terminal.

  • Hans

    I get two errors:
    “studio..h” No such file or directory
    “Warning” incompatible declaration of built-in function ‘printf’

    Does ‘studio.h’ have to be on the desktop as well? Where is it located?
    Thx Hans

    • PFB

      @Hans
      I assume by now you found that it isn’t ‘studio.h’ but ‘stdio.h’. It stands for STandarD Inpout/Ouput, which is what you need to print messages to the screen (if that’s your “standard output”, that’s a long story, hence just saying ‘screen’).

      You see: “Warning” incompatible declaration of built-in function ‘printf’
      You do: add this exactly at the beginning of your code: #include

  • Kirk

    Hey, I never ever touch any progamming stuff, I would like to become an app developer of apple. What should I learn 1st? I want my basic to be strong… And, how do app actually be developed? By individual, or a team? I heard that an app should be created as a team, like the man team, and I want to create a really professional app, is it necessary must be a team? And, if we program as a team, shall we specialised on the are we want to do, or we all still have to learn the same progamming. And too, please basically how they linked thing together( between graphics, picture) And what is the limitation of its device. I have a lot of question, I really wanted to learn, somebody can teach me by actually having lessons and stuff for me, please…..

    • http://www.danwalker.com/ Dan

      Hi Kirk,

      Team work or individual work is entirely your preference. I tend to develop some things on my own which are smaller projects, for larger projects I generally work with a buddy or small team.

      I’d say it’s better to start on your own and get competent before you start working with a team. You don’t want to be the one lagging behind, you also don’t want to be the one learning bad habits from other programmers. The great thing about developing for iOS is the sheer amount of resources, with a little googling you can find a tutorial for pretty much any aspect you want to know more about.

  • George

    Hey how are you, i am currently looking for a place to go to learn how to write scripts and learn coding is there place in sydney where i can do this? thanks.

  • http://peacemakerdawg.wordpress.com/ Wasim Sandhu

    This is a great tutorial for any Objective-C beginner. And I can see that it took an abundance of effort to write this. Nice job!

  • Alan

    Hi,

    Is there any chance of anyone making any videos for these tutorials as i am totally confused, also are there any other basic tutorials available as i am not even able to get the Hello World application to work properly. Great tutorial though, learnt alot.

  • Jonathan

    Hello,

    I can’t continues to this tutorial because i get an erro “gcc command not found” the version of xcode is Version 4.3.2 (4E2002) and my mac is 10.7.3, why i get same error every time that i try ?

    I need some help to figure out how to resolve it, my variavel $PATH of SHELL is exists.

    Some tips ?!

    I install yesterday the xcode and all going fine!

    The Best,
    Jonathan

    • John

      I am having the exact same problem, just installed lion and xcode 4. Did we miss something?

  • http://sdbwebsolutions.com Serg

    Great Tutorial Dan! I’m definitively sharping my objective c skills. Will create more posts hopefully with positive results about the challenges.
    Looking forward to work hard!

  • http://sdbwebsolutions.com Serg

    I’m definitively sharping my objective c skills. Will create more posts hopefully with positive results about the challenges.
    Looking forward to work hard!

    Thanks Dan!

  • lukas

    i dont really understand how to compile with the the terminal thing and xcode. How do u do that?

    • Ro

      All you need to do to compile in XCode is hit the “play” button at the top. It will check your code and run it if there are no errors.

      HTH!

  • http://twitter.com/DiegoDoes Diego Segura

    Hi, I’m Diego, and love this tutorial. It’s my first attempt at learning, really. I don’t think age matters, like Dan said, he started at twelve, I started a few weeks ago (age 11). In regards to earlier comments in the thread, I don’t believe HTML is a language, nor a programming language. I think it’s purely for design. Nonetheless, It’s where I started 2 years ago, and coming into languages like Javascript, C and Objective-C, it’s given me the slightest bit of an understanding. I’ve learned a lot about C/Objective-C in the past weeks, and this section (Day 1) was just a bunch of review for me, but did help me understand things better. Great tutorial! Also, I was wondering if anybody here knew of a good site with Photoshop techniques, as I always would like to expand my skill-set. Good luck with Objective-C everybody!

  • John

    Hello, I’m 13 and I am highly interested in programming. I sort of learned visual basic 2010 and made bunch of game, releasing it to my friends. I am also highly interested in virus stuffs. I made numerous malwares EXPERIMENTALLY (never used it to somebody except myself ACCIDENTALLY) :(
    I am not sure what to do for my future. I think I should learn real programming besides visual basic, but I don’t know what language I should learn. C? Python? Object C? What should I learn to be a successful programmer?

  • Mary

    Hi I tried doing all 3 loops in Xcode 4 and the if Loop worked ok but the while and do are coming up with (lldb) in my output.

    Thanks for the tutorial I am just starting out and did day 1.

    Mary

  • John

    Hey, Im interested in programming and im currently in college taking comp sci classes and everything. But I just started and I used MatLab and I just finish learning Java. Should there have been other languages I should of learn first? If so where should i start. Im on summer break from football and school so I trying to use my time learning and working out before I go back for summer school.

  • shreya

    i have windows xp.. so on wt should i work out..
    i mean wt should i install to run d programs..

  • Reid

    Note fo those using Lion:

    The “gcc” command is not valid until you download Command Line Tools. Go into XCode, preferences, downloads. Click the “install” button to the right of “command line tools” at the bottom of the window.

  • Mohammad

    Hi thanks a lot, but where can i find the second day class ?

  • Mohammad Rifqi

    nice you help me a lot :D…

  • JOHNREY ANGOLLUAN

    THANKS!!
    I really want to learn other programming languages aside form visual basic and java.

    • rob v

      Forth

      They will tell you its an old forgotten language, but don’t believe it. Many companies including Apple like that to be the general perception because its the secret sauce to their viability in speed of development and secrecy/security, because of its stack operations and Reverse Polish Notation makes code look backward if decompiled.

      ( think Yoda speaking ) Ja Ja binks would say 1 plus 2 equal 3, Yoda would say 1 and 2 added, 3 the equal is )

  • Lisa

    I am a beginner in coding, I know little bit of python(just basic). I am taking curse on IOS development, for the first few classes I understand the over all ideas but not the details of the codes. I am not sure if I should keep going done this path. advices please !!

  • Lisa

    should I learn some objective-c first or learn more python before go into IOS development. if i learn objective-c, should I learn about C first? some people think C is necessary before learning objective-c, some people disagree. what you all think?

  • Liam

    Hey man, Love this !

    I get the rest and am really enjoying reading through and learning, but I dont understand the point of ‘return 0;’. I know integer must be a whole number, but why not 1 or any thing else. This is just for the top part.

    Thanks.

  • Samuel

    Hi,
    I am just doing this for the first time, but everytime I try to compile with the terminal, I get the message “-bash: gcc: command not found”

  • Mitchell

    To everyone saying how old you are its irrelevant if you have an interest and you’re willing to try and learn you can, age has no impact…

  • Jaja

    Hi Dan

    I am having getting started with this. I have x code and am trying the gcc command but every time do so I get : i686-apple-darwin11-llvm-gcc-4.2: program1.m: No such file or directory
    i686-apple-darwin11-llvm-gcc-4.2: no input files

    I am installed command line tools but am getting this result. I am eager to learn but cant get passed this :(. Any help would be great.

    • mee

      me too.. that’s really suck:(

  • foysal

    I dont have mac so please tell me how could i run obj c on windows 7?…

    • Safeburn Safedll

      hello you can learn but you cannot compile obj c on windows.. but you can install mac os x as a virtual machine , and working on _VM,