003 Intro to Objective-C

- DRAFT CONTENT -

Pointers

Pointers are like addresses to the location of the cubbyhole where you can find data.  The reason you might use a pointer instead of a variable is you only have so much room near by to store data, so an address to data takes much less room.
Store the value of 3 in a variable named myVar
int myVar = 3;

// myVarAddress is a variable that holds a pointer (a address) which is signified by the *.  This means that this variable only holds addresses to cubbyholes of the type int

int *myVarAddress = address;  

Classes and Objects

Classes are like factories, they create new copies of objects.
Objects are like digital clones or models.
Let’s start with an example.  UILabel is a class.  To use UILabel you have to create an instance of the class called an object.  You can create an instance of a object by using alloc then init.  You can only refer to objects through pointers.
This statement creates an instance of the UILabel class and stores a reference to it in a pointer variable named label1
UILabel *label1 = [[UILabel alloc] init]; 
these are two labels, two instances of the same class (UILabel)
the class defines properties or traits of a class, and methods (things it can do)
let's take a look at the UILabel documentation;
some of the properties of UILabel includes, the text and the text color.
let's see how to changes these properties in the code.
show video
- objects
- methods
- variables, properties
//

add label in interface builder

- how to get to label
-
// look at objective c primer

002 Intro to C

You’ve just created your first iPhone app. Now we’re going to dive deeper into how an app works. We’re going to start by learning to write source code (computer instructions) or code for short, in a language called C. C is the father of Objective – C, which is the language you’re going to learn in the next tutorial.

Let’s get started!

Variables

Variables are like cubbyholes for your data.  You can store data in variables and you can retrieve data from variables.

There are many different types of variables including variables for numbers, strings, sounds, and images.  The list goes on and on.  You can store just about any type of data in a variable of the correct type.

Creating a variable

A variable can not be used unless it has been created.  Creating a variable is like labeling a cubbyhole with your name to reserve it for your use only, so nobody else will accidentally put something in it.

To create a variable use a command formated like this:

variable_type variable_name;

int myVarA;

This command creates a variable named myVarA of the type int (short for integers).  This type of variable stores signed number data.  In other words it can store both positive and negative numbers.

Did you notice the ; at the end of that statement?  You must end all statements  (a line of computer instruction) with a ; (semicolon).

Storing data in variables

You can store data in a variable.  Assign data to the variable using the assignment operator = (equals sign).

= places the results of the right side into the variable on the left side.

myVarA = 3;

this command stores the data value of 3 in the variable named myVarA

Retrieving data from variables

You can retrieve data from a variable by using the variable name.

myVarB = myVarA;

stores a copy of the value in myVarA in myVarB.

To give you an example in the real world using our cubbyhole analogy.   Well okay maybe in Star Trek world it would be like this.  I had put a book in a cubbyhole called myVarA.  After the previous statement another cubbyhole called myVarB would have an exact copy (a replication) of that book.  That statement doesn’t move the book, it replicates the book stored in the cubbyhole labeled myVarA and stores the copy of the book in another cubbyhole labeled myVarB

Functions

Functions are a programmer’s general purpose calculator.  They work on all kinds of data, not just numbers.

Using a Function

You use a function by making a function call:

function_name(input1, input2);

NSLog(@"Hello World!");

The purpose of the NSLog function is to log (print) out information in the terminal.  A function in general can accept zero or more input parameters.  The NSLog in particular can accept one or more input parameters.

The terminal is a area where our program can print information as it’s running, we’ll use this as we’re developing the program to help us debug (find flaws) or understand what’s going on before the program is complete

Let’s try this now. Follow along while watching this video.

Creating your own functions

output_type function_name(input_type input_name, input_type2, input_name2) {

function_commands;

}


int add(int myVar1, int myVar2) {

    return myVar1 + myVar2;

}

The add function adds the two parameter passed in as myVar1 and myVar2 and returns the value to the code that called it. When you call the function you want to store the output of the function somewhere so you would call the function with a statement like this:

myVar3 = add(3, 2);

This statement calls the add function with the parameters 3 and 2 and puts the result in myVar3. At after this statement myVar3 will have the value of 5;

Comments

Comments are notes written by the programmer to help explain or summarize code that is hard to understand, it is ignored by the computer but it helps you or other programmers who’s working on the code understand it.  Comments have the following syntax:

// some comment that summarizes the complicated code below
lots of really really complicated code;

// show console

// show Xcode documentation

001 Hello World!

Prerequisites
Have Xcode 4 installed.  If you haven’t done that, please read this post and come back.
What we’ll cover
 We’re going to build a simple app and run it in the iPhone simulator.  We’ll walk through all the tools you’ll use to build iOS apps including Xcode 4, interface builder, and the iPhone simulator.  We’ll go through the workflow used to create an app; that is making changes, building, and running the app.
 
Homework
Explore the other widgets in the Object Library.  Add these widgets to your interface and run the app!

000 Install Xcode

To begin app development you’ll need Xcode, a free tool you can download directly from the Mac App Store.

  • Go to Mac App Store and download Xcode
  • Go to /Application and run “Install Xcode”
  • use Spotlight to find and run Xcode
  • use the command “Keep in Dock” to pin Xcode to the dock so you can launch Xcode easily

Prerequisites

Familiarity with how to use computers.  You should know how to navigate, work with files, do basic word processing, use a web browser, etc.
You’ll need a Mac with OS 10.7 (Lion) installed
  • I’ve tried to bring down as many barriers to starting iOS (iPhone, iPod, iPad) development as I can and owning a Mac is a common one.  Now you can start iOS development before you commit to buying a Mac.  You can rent access to a Mac.  See this page for more information.

Structure of Tutorials

The tutorials are written so you succeed fast.  The material will be covered in much the same way as a painter paints, it’ll be in broad strokes so you can get the big picture fast.  We’ll go in and fill in some of the more obscure details in later tutorials.  I find this is a great way to keep the learning fun and make the vast amount of information manageable.

As we’re covering code you’ll find code snippets in boxes similar to what you see below:

code snippet