2011年10月10日星期一

Chapter One: Introduction to Programming

Chapter One: Introduction to Programming:

Programming is the process of giving instructions to a computer. Apps on your iPhone work because someone took the time to write instructions that have been packaged into an app that you download and use on your iPhone.


Here’s an example of how you would give instructions to an iPhone app. The code below will show us a alert that says Hello World:


UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert View"
message:@"Hello World"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];

The code above will present an alert view to the user with the text Hello World. See figure 1.1 below for an example.


Figure 1.1 - Hello World


These instructions probably don’t make much sense to you yet but that’s ok – this book is all about how you can use instructions like this to get things done in your apps.


Programming Languages


Programming languages are used to communicate instructions to computers like the iPhone. Like natural languages, programming languages have a vocabulary and must follow certain rules. We must follow these rules strictly so that a computer can understand what we intend.


Writing instructions with programming languages is called coding or sometimes simply writing code. Coding for iOS apps takes place with an editor called XCode that is specifically tailored to make writing code easier. The programming language that we will be using and learning about is called Objective-C. The first half of this book is about learning the rules and vocabulary of Objective-C because that is what iPhone apps are made of.


Programming Essentials


Let’s go over some the essential rules and things you can do with programming.


Code Execution


Code execution refers to when instructions are given to the computer. Computers execute instructions one at a time as they are received. When you are looking at a screen full of code, each line of code will get executed one at a time from top to bottom in the same way a person would read a book. Top to bottom. Left to right.


A line of code is an instruction that is ended by a semi-colon ;


For example:


[alert show];

It is very important to remember the semi-colon when you end a line of code.


Code Regions


Code regions are used to separate a set of instructions from the rest of the program. You will see this in different contexts as you learn programming but the general idea is that code that is in a code block belongs together. Code regions start with a left curly brace { and end with a right curly brace }.


Here is an example of curly braces defining a code region:


if(alertView){
NSLog(@"UIAlertView was used");
NSLog(@"as an example");
NSLog(@"and it was great.");
}

Code Documentation


As you get into coding you may start to lose track of what your code was supposed to do. One way to remind yourself what you were trying to accomplish is to document your code. This just means to write a short note called a comment that is included with the rest of your code.


This code comment will let others know what you were intending on doing but it won’t be considered an instruction to the computer. Code comments are ignored. You can use a comment anywhere in your program by enclosing your comment text with these characters: /* and */.


Here is an example of code documentation:


/* This program is going to calculate monthly loan payments
for auto loans with different interest rates and payment schedules */

This type of documentation works best when applied to a big section of your program where we want to remember what the broad overview of what we are doing. To document smaller pieces of your program it is better to apply this notion of self-documentation. What we mean by self-documentation is that the words we choose for each piece of the program should describe what are intentions were. For example, take a look at these two lines of code:


float x = 199;
float interestRate = 4.25;

Even though you do not know the programming syntax that we are using here yet I bet that you can guess what the second line of code is supposed to be. The programmer probably choose the word interestRate because the code had something to do with interest rates right? Who knows what x is supposed to be? Compared to x, interestRate is much more meaningful. Hopefully, if you remember to write your code like you will save yourself some work down the road.


Debugging


Debugging is the process of finding mistakes in your code and fixing them. In practice, more time is spent on debugging than coding itself. Generally, when I am writing some code I will quick type it out and then try to run the program, see if what I did works and then test the results. Usually, I will need to go back and fix something. In fact, I would say that most programmers spend 90% of their programming time debugging. This is why I want to now show you how XCode makes this process as painless as possible for us.


Debugger Screens


The debugger is a special area in XCode that has the tools you need to debug your program. To see the debugger select View > Debug Area > Show Debugger Area. You should see a screen like Figure 1.2 appear at the bottom of XCode.


Figure 1.2 - XCode Debugger


In figure 1.2 you can see that I marked the most important sections of the debugger. Area 1 is used to control the app’s execution when it is running in the simulator. You can pause and resume the app. This figures in more when we talk about setting breakpoints below. You use area 2 to inspect your code while the app is executing. This is what you use when you want to see what is happening while your app is running.


The area marked 3 is the console. This is a special window that the program will write messages to giving us status updates. We can write message to this window ourselves using NSLog. In area 4 you will find a control that you can use to show or hide the various debugger panes.


NSLog


NSLog is used to write messages to the console screen. This is a simple way to see what is going on in your program and it is also a way to produce output. Output is simply information created by our program and presented to us. You use NSLog like this:


NSLog(@"This is my message");

Everything that you include in the parenthesis will be printed out to the console. You can also include substitute other items into your NSLog messages. What you need to do is insert special placeholders into your NSLog messages followed by a comma-separated list of things to substitute into your message. For instance, if we wanted to add our alert object from the Hello World example into our message we would do this:


NSLog(@"This is my alert object: %@", alert);

Setting Breakpoints


Breakpoints are flags that you can attach to a line of code that will stop your app from executing when that line of code is reached. When the app stops you get a chance to use the debugger screen to look at your code to see what is going on. To set a breakpoint all you need to do is click in the gutter (the area right to the left of the code editor). The gutter is the area marked in red in figure 1.3. Also, if you look closely you will see a blue icon which is an example of a breakpoint.


Figure 1.3 - Breakpoints and the Gutter


Now when I build and run this app everything will stop when the program reaches the blue breakpoint. If I look at the debugger screen I’ll see tons of information about my program in it’s current state and I will see the message I wrote to the console screen.


You can see all kinds of information on this screen, as you learn more about programming it will become more meaningful to you.


Summary


Programming is how we get computers to do the tasks required for iPhone apps. The thing with programming is that we need to be very literal and strictly follow the rules of the programming language that we are using to get our work done. XCode is the tool that Apple provides developers to help us get our work done and becoming intimately familiar with this tool will help you out a lot. In the next chapter we start going over the essential programming rules that we use to get things done in iPhone apps.


Please note that this chapter is just an introduction to the tools and general process of programming. The remaining chapters of this book will make the process of programming much clearer so please stick with me and jump right into chapter two where we talk about if statements.

没有评论:

发表评论