IPhone Development Basics (Part 1)

Object Oriented Programming Overview

OOP Vocabulary:
  • Class: defines the grouping of data and code, the “type” of an object
  • Instance: a specific allocation of a class
  • Method: a “function” that an object knows how to perform
  • Instance Variable: a specific piece of data belonging to an object
  • Encapsulation: keep implementation private and separate from interface
  • Polymorphism: different objects, same interface
  • Inheritance: hierarchical organization, share code, customize or extend behaviors
Cocoa Framework
Cocoa Framework


Objective-C:
Objective-C was created in the 1980s and is an extension of the C language. It adds many additional features to C and, most important, an OOP structure. Objective-C is primarily used for developing Mac and iOS applications and has attracted a devoted group of fol- lowers who appreciate its capabilities and syntax.

Objective-C Programming Basics
This section describes the basic Objective-C Syntax:
  • Declaring variables.
  • Allocating and initializing objects.
  • Using an object’s instance methods.
  • Making decisions with expressions.
  • Branching and looping


Declaring Variables: Instance variables store information that is available across all the methods in your class. Most commonly, you’ll be declaring several variables at the start of your methods, using them for various calculations, and then getting rid of them when you’ve finished with them.

Whatever the purpose, you’ll declare your variables using this syntax:
<Type> <Variable Name>;
The type is either a primitive data type or the name of a class that you want to instantiate and use.

Primitive Data Types: Primitive data types are defined in the C language and are used to hold very basic values. Common types you’ll encounter include the following:
  • int: Integers (whole numbers such as 1, 0, and -99)
  • float: Floating-point numbers (numbers with decimal points in them)
  • double: Highly precise floating-point numbers that can handle a large number of digits

Object Data Types and Pointers: To declare a variable as an object of a specific class, we must declare the variable as a pointer to an object. A pointer references the place in memory where the object is stored, rather than a value. To declare a vari- able as a pointer, prefix the name of the variable with an asterisk. For example, to declare a variable of type NSString with the intention of holding a user’s name, we might type this:

NSString *userName;

Once declared, you can use the variable without the asterisk. It is only used in the declaration to identify the variable as a pointer to the object.

Allocating, Initializing, and Releasing Objects
Before an object can be used, memory must be allocated and the contents of the object initialized. This is handled by sending an alloc message to the class that you’re going to be using, followed by an init message to what is returned by alloc. The syntax you’ll use is this:
[[<class name> alloc] init];

For example, to declare and create a new instance of UILabel class, you could use the following code:
UILabel *myLabel;
myLabel=[[UILabel alloc] init];

Once allocated and initialized, the object is ready to use.

Convenience Methods: When we initialized the UILabel instance, we did create a usable object, but it doesn’t yet have any of the additional information that makes it useful. Properties such as what the label should say or where it should be shown on the screen have yet to be set. We would need to use several of the object’s other methods to really make use of the object.

Apple’s classes often provide a special initialization method called a convenience method. These methods can be invoked to setup an object with a basic set of properties so that it can be used almost immediately.

For example, the NSURL class, which you’ll be using later on to work with web addresses, defines a convenience method called initWithString. To declare and initialize an NSURL object that points to the website. we might type the following:

NSURL *iPhoneURL;
iPhoneURL=[[NSURL alloc] initWithString:@”http://www.teachyourselfiphone.com/”];

Without any additional work, we’ve allocated and initialized a URL with an actual web address in a single line of code.

Using Methods and Messaging: To send an object a message, give the name of the variable that is referencing the object followed by the name of the method—all within square brackets. If you’re using a class method, just provide the name of the class rather than a variable name:
[<object variable or class name> <method name>];

Things start to look a little more complicated when the method has parameters. A single parameter method call looks like this:
[<object variable> <method name>:<parameter value>];

An actual example of using a multiple parameter method looks like this:
NSString *userName;
[userName compare:@”John” options:NSCaseInsensitive];

Expressions and Decision Making: Every decision in an app boils down to a “yes” or “no” result based on evaluating a set of tests. These can be as simple as comparing two values, to something as complex as checking the results of a complicated mathematical calculation. The combination of tests used to make a decision is called an expression.

Making Decisions with if-then-else and switch Statements: The most common way of defining these different execution paths is with an if-then-else statement:

if (<expression>) {
// do this, the expression is true.
}
else {
// the expression isn’t true, do this instead!
}

For example, consider the comparison we used earlier to check a userName NSString variable to see whether its contents were set to a specific name. If we want to react to that comparison, we might write the following:

If ([userName compare:@”John”]) {
userMessage=@”I like your name”;
}
else {
userMessage=@”Your name isn’t John, but I still like it!”;
}

A switch statement checks a variable for a value, and then executes different blocks of code depending on the value that is found:

switch (<numeric value>) {
case <numeric option 1>:
// The value matches this option
break;
case <numeric option 2>:
// The value matches this option
break;
default:
// None of the options match the number.
}

Applying this to a situation where we might want to check a user’s age (stored in userAge) for some key milestones and then set an appropriate userMessage string if they are found, the result might look like this:

int userAge=18;
switch (userAge) {
case 18:
userMessage=@”Congratulations, you’re an adult!”;
break;
case 21:
userMessage=@”Congratulations, you can drink champagne!”;
break;
case 50:
userMessage=@”You’re half a century old!”;
break;
default:
userMessage=@”Sorry, there’s nothing special about your age.”;
}

Repetition with Loops: A loop defines the start and end of several lines of code. As long as the loop is running, the program executes the lines from top to bottom, and then restarts again from the top. The loops you’ll use are of two types: count-based and condition- based.
In a count-based loop, the statements are repeated a certain number of times. In a condition-based loop, an expression determines whether a loop should occur. The count-based loop you’ll be using is called a for loop, with this syntax:

for (<initialization>;<test condition>;<count update>) {
// Do this, over and over!
}
Example
int count;
for (count=0;count<50;count=count+1) {
// Do this, 50 times!
}

In a condition-based loop, the loop continues while an expression remains true. There are two variables of this loop type that you’ll encounter, while and do-while:

while (<expression>)
{
// Do this, over and over, while the expression is true!
}
and

do {
// Do this, over and over, while the expression is true!
} while (<expression>);

For example, suppose you are asking users to input their names and you want to keep prompting them until they type John. You might format a do-while loop like this:

do {
// Get the user’s input in this part of the loop
} while (![userName compare:@”John”]);



Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment