iOS For High School Students: Getting Started
This is a blog post by Connor Osborn, an aspiring student with a passion for creating software and 2d games! This is a new tutorial series on beginning programming for iOS – for high school students, by high school students! We’ll walk you through creating an app from the very beginning, assuming zero knowledge! And we’ll make […] By .
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
iOS For High School Students: Getting Started
35 mins
- Getting Started
- Understanding The Hello World Application
- Creating Our Own Application
- Variables
- Printing Statements
- Using Scanf For Input and Assigning Variables
- Programming with Logic: If Statements
- Solving Problematic Inputs with While Loops and Else Statements
- The Infinite While Loop
- Where To Go From Here?
This is a blog post by Connor Osborn, an aspiring student with a passion for creating software and 2d games!
This is a new tutorial series on beginning programming for iOS – for high school students, by high school students! We’ll walk you through creating an app from the very beginning, assuming zero knowledge! And we’ll make things tons of fun along the way. :]
You want to learn to develop iPhone apps, eh? Well, learning how to program is the first step.
In this tutorial, we’ll get started with the basics of programming in Objective-C and create a simple Mac app called “Are you a WIZARD?”
“Wait a minute,” you might say to yourself. “Why are we making a Mac app? I wanted to make an iOS app!” Well, making a command-line Mac app is a nice and easy way to experiment with the basics of programming before we get into more complex subjects.
To follow along with this tutorial, you’ll need a Mac (preferably one that is not super-old) and Xcode. You can download Xcode for free from the Mac App Store!
Getting Started
Click Setting Up Your Programming Environment for a visual guide to setting up Xcode. Otherwise, follow the instructions below. :]
Step One. Open Xcode and select the option to create a new Xcode project from the popup, or select File\New\New Project from the menu.
Step Two. Choose the Command Line tool by selecting Mac OS X>Application>Command Line Tool.
Step Three. Enter “Are You A WIZARD?” for the Product Name. Change the file type to: Foundation. Save the project somewhere you can easily find it!
Step Four. On the left side of the Xcode window is a simple file hierarchy. The file main.m is the only thing we are going to work on, so click it to open it. You should see the following code:
//
// main.m
// Are You A WIZARD?
//
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@"Hello, World!");
[pool drain];
return 0;
}
You’ve done it! You have created your first application! Excellent job! :p
Understanding The Hello World Application
As you may have figured out, your new project has a simple program pre-coded as a template. It is known universally as “Hello World!” and has been the traditional “first program” for beginning coders for decades!
Press the Run button in the upper lefthand corner. By clicking Run you are actually telling Xcode to build (compile all files into a single program) and run the program. You should see the following output in the lower part of the screen:
Hello, World!
Here’s a screenshot showing where you can find this (and I’ve circled the buttons you should click to bring up the console panel if it isn’t open already):
Now I’ll explain each line in the Hello World program. Armed with that knowledge, we will move straight to the “Are You A WIZARD?” game.
The first section of code, shown below, is not actually part of the program! It is completely ignored by the compiler.
//
// main.m
// Are You A WIZARD?
//
How does the compiler know to ignore these lines? The key is the double forward slash marks (“//”) at the beginning of each line. They tell the compiler, “Nothing to see here, move along.”
Programmers use // lines to document their work by writing comments into the code that do not affect the program. We will use some in a little bit.
The next statement, below, simply means “go grab Foundation/Foundation.h, because it has some stuff we need for the program.”
#import <Foundation/Foundation.h>
We could import any header file with the import command. However, Foundation.h (foundation.header file) is the one we need for our command line application.
The next section, called the main function, is the heart of the program. It is all contained within brackets {}.
At the beginning of every program you will have to import various files (such as Foundation.h), define methods, blah blah blah. However, after all of that infrastructure is brought together, the main {} is where you begin to execute each step.
int main (int argc, const char * argv[])
{
}
Still confused? Well I’ll try a super lame analogy to explain this, so bear with me. :/
If you were going to bake a cake, you would first gather all the ingredients and tools you needed, right? You might get the eggs out along with the milk, pan, egg beater, etc.
Even if you didn’t put all these things on the counter right away, you would at least scan the list of ingredients to see what you needed!
Well, the ingredients and tools are like the header file (Foundation.h), and the recipe is like the main function, or everything within the main brackets. I hope that helps!
Now we will go through the main function line by line. Note: Everything within the brackets {} is called a block statement. Every statement within a block statement requires a semicolon “;” at the end.
The first line allows our program to use (borrow) memory from the computer for our application. Don’t worry if this is confusing, just think of it as something you need to add at the start of a program for now. All will be clear in the future. :]
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Careful reader, you ought to know what this next line is: just a comment! As we discussed earlier, the “//” instructs the compiler to ignore the line.
// insert code here...
Next we have our first function, NSLog(). A function is a block of code that performs a specific task. It has a name and it is reusable.
NSLog(@"Hello, World!");
NSLog() is a function with a very simple task. It displays everything between the first and second double quotation marks, or as we say, it displays a string (a list of characters), which is exactly what the statement “Hello, World!” is.
Note the quotation marks has a little @ sign beforehand. You need to put this whenever you use a string in Objective-C. It’s easy to forget to add this, but if you do your program will crash, so be careful!
After NSLog, our program has completed its purpose. However, we have borrowed memory in an autorelease pool, and everything that required memory is now finished. So, the line below instructs the computer to drain the “pool” of memory so that the device can use the memory elsewhere.
[pool drain];
Always releasing memory at the end of a program is a good habit to have as an iOS developer! Although the applications we write will not be memory intensive on a Mac, iOS devices have very limited RAM. Every time we borrow memory from the device and forget to release it, that memory cannot be used for other important tasks.
Finally, the last piece of code! “Return 0” terminates the main function by requiring the console to return the value 0, if everything was successful.
return 0;
Go ahead and click Run again. The final line of output should read “Program ended with exit code: 0.” If you get code: 0, then everything ran correctly.
Now let’s try something. Go ahead and substitute the value 3 for 0 in the last line of your main function (“return 3;”) and click Run again. You should understand that the command returns whatever you want to use to test that the program ran correctly.
Phew! Hopefully this coding business looks a little less intimidating now. But that code was provided for us, and all we had to do was press Run. NEXT, we begin creating our own application involving wizards and special powers!