Language of Choice - Part 2
Language of Choice - Part 2
Language of Choice - Part 2
So, in the last section we briefly went through what programming is all about, as well as the key terms you will come to work with as a programmer. This time we will talk a bit about the language which we - here at Pixeleap - chose as the language for the series, as well as how to install and set up the Integrated Development Environment (IDE) that we will be working with. We will also finally be looking at some code, although it will be from the next part in the series that the articles will be focused completely on programming. It is important to get you ready with the right tools first. Objective-C as the language of choice For this series we have chosen to use Objective-C as the programming language of choice. This means that all theory I cover throughout the rest of the series will be specific to Objective-C, and programming examples will happen through Xcode (the IDE used in the Mac OS X environment, but more on that later). If you want to use Xcode and Objective-C, this means you will need to have an intelbased Mac. Objective-C is a superset of C, which means that it is actually build on top of the programming language called C, and adds extra functionality and the object-oriented programming paradigm. This also means that some things you will be learning through objective-c (like syntax and other concepts) will be exactly the same or close to the same thing in C. If you are not able to use Objective-C, you will still be able to follow parts of the series that discuss the more underlying concepts behind programming, and even directly apply much of the syntax or concepts to languages such as C. Just be aware that when I go through explanations of how Objective-C is build up, use data types, and so forth, other languages might do those things differently.
Intalling Xcode and the SDKs All right, so lets get you all set up for some actual programming! First of all you will have to download and install Xcode as well as the latest Mac OS X- and iOS SDKs. Luckily this is all extremely easy! Open Safari or whatever browser you are using, and navigate to http://developer.apple.com and from the main page go to iOS Dev Center, from there you will see the download for Xcode, and it is already bundled up with all the SDKs you need. Simply download and install and return to this tutorial when you are done. Introducing Xcode So, first of all lets fire up Xcode (You will find it in your applications folder)! The very first thing Im going to do is show you how to start up a new project.
Launching up Xcode should give you this very pretty welcome screen. Now what were interested in here is the option that says Create a new Xcode project, so go ahead and click that now. The next thing you will see is a template selection screen, which is where you can get Xcode to set up some code templates for you, depending on which type of application youd like to develop. For this article we will focus on a simple command line tool, so go ahead and navigate to the Mac OS X category -> Application and then choose Command Line Tool and click next. Here is a picture to illustrate the correct selection:
Right after clicking next you will be presented with a window wanting you to name your new project, as well as choosing a type, like this:
Just leave the type as it is, at Foundation, and name the project HelloWorld (yeah, who would have guessed?). Click next and we will finally be presented with the simple project template, and a simple hello world example. When selecting the main.m file from the file browser to the left, you should now have a screen looking a bit like this:
You are now looking at the IDE. Before we go any further, I want to make a quick walk-through of the most regular and important tools you will be using as a new programmer, and talk a bit about Objective-C so you get a quick idea of the language were going to be working with. Lets start by taking a quick tour in Xcode, mainly how to compile your program. If you are unsure about these concepts you can turn to the first part of this series, which explain them. First of all we can try and compile the current hello world program provided by Xcode. Dont worry about the actual code right now; well get to that soon. To compile the program, simply hit the big button in the top left that says Run and looks like a big playback button. This button will compile your program and run it, if compilation is successful. When you run it, you should get a small console output window in the lower right of the IDE, looking like this:
If you look in the console output, you can see the bold text saying Hello, World!. This is the text coming from our program. So there you go, you just compiled and ran a program! All right, might not have been all too exciting, and nothing much happened, but everything has a start. Now let us take a quick look at the code. Currently our project consists of this short piece of code
Now theres actually a bit of concepts here that will be explained later in the series that we will more or less ignore for now, as they require a bit deeper explanation to completely make sense. In the next part of the series will go delve straight into actual programming, this part is mostly about getting you all set up with Xcode and letting you get a feel of the environment that well be working with. If you look at line 11, what you see there is whats called entry point of the program, and is a function called main. This is a function that will automatically be called and run as the very first thing that happens when your programs are run. A function is essentially a block of code that is named, and the code that makes up the function is everything between the brackets on line 12 and 21. So when our program is run and our main function is run, all the code inside this function will be run. For now we will ignore the part of the function name you see after main. The concepts of functions will also be explained in much greater detail in later parts of the series. Go into the function and the bit we will focus on for now is line 17, dont worry about the other details just yet, those will be introduced at later stages. NSLog(@Hello, World!); This is the line that prints out a message to the console. NSLog is also a function, and when you write the name of it in your code like above, it means that you want to call and run whatever code is inside that function. You might be asking yourself where the function is coming from, but it is written in another code file and imported for you to use. The bit that goes between the two parentheses is what is called a parameter or argument. In this case, it tells the NSLog function what you want to have written out in the console. Feel free to change it around, or even add another lines similar to that with other messages, for example: NSLog(@This is my own message!); Just remember to put the @ before the text. This means that you want to work with a special kind of objective-c defined string(strings will be covered next time). So that was a quick look at the code, dont worry if you dont grasp the concepts and terms yet, this part of the series was not meant to explain it, just give you a quick taste. Next time we will start out with actual programming where I will go over the concepts of variables where we will go into actual coding examples so you can get your hands dirty!
File extensions A quick note on the file extensions you will most often see when working in Objective-C and this series. All of our source code will be contained within files ending with the extension .m and h. Files ending with .m is the Objective-C source code files. Files ending with .h is header files. Header files will be introduced when we start working with functions and objects and classes. Conclusion Whew! So this time we got you all set up with the tools you need to start out programming, and also made a quick introduction to the language that you will be working with (Objective-C), and the environment you will be doing it in (Xcode). We also quickly went through some Objective-C code so you could see a bit of what its all about. In the next part of the series we will go straight into actual programming theory and practical examples, when I will be going over the concept of variables. See you all next time! And of course are more than welcome to leave comments or write me an email if you have questions or any kind of criticism. -Kasper (Kasper@Pixeleap.com)