iPhone Apps: Objective C Pt 3

Data Type’s in Objective C

In this article I’ll go over all of the data type’s available to you with Objective C. They include:

• int: Positive or negative number’s, without a decimal point
• float: Positive or negative numbers, with a decimal point
• double: Large floats
• char: Single character’s
• id: Generic datatype used to store a reference to an object. I’ll cover this in detail later.
• arrays: Think of an array as a big box, with many boxes inside of it. Each of the boxes inside the big box contain a value.
• strings: A string of characters

Integers

You have seen this before in the previous tutorial’s. An integer stores a positive or negative number, without a decimal. You can trust that you will be able to store numbers up to 2,147,483,647, in an int.

There are different version’s of the int data type. Here I’ll list them. First I’ll show you how to declare them. For example a short int would be defined like this short int someNumber; I’ll then list the maximum value they can hold. And, then list the code you would use with NSLog. Example using NSLog with an int: NSLog (@”Here is an integer %i”,intNumber);

• short int: 32,767 : %hi
• unsigned int: 4,294,967,295 : %u
• long long int: Machine dependent, but at least 9,223,372,036,854,775,807 : %lli

Floats

Floats are used to store numbers that might require a decimal. Objective C by default stores all decimal numbers in the data type double. If you want to force it to store in type float you would do that by appending the letter f to the end of the number like this declaration:

float numWDec = 123.45f;

You can store numbers expressed in scientific notation in a float. You would do that like this:

float sciNot = 1.23e5f;

If you want to use a float in NSLog, you would use the notation %f. If you want to use scientific notation, you would instead use the code %e.

Doubles

Variables of type double are guaranteed to be accurate up to the value of 9,223,372,036,854,775,807. They can contain decimal’s and are the default data type for storing these types of numbers.

The code for using a double in NSLog would be the same as float’s which are either %f or %e.

Characters

If you want to store a single character in a variable, you would declare that variable to be of type char. You define what character is stored in the variable, by surrounding that character with single quotes, like this:

char theChar = ‘D’;

You can also store special character’s in a char data type, by instead storing a backslash code such as:

• \a – Sound alert
• \b – Backspace
• \f – Form feed
• \n – New line
• \r – Carriage return
• \t – Horizontal tab
• \v – Vertical tab
• \\ – Backslash
• \” – Double quote
• \’ – Single quote

The use a char variable in NSLog, you would use the code %c.

id

Remember from the previous tutorial when we asked the computer to set memory aside for us, to store our new object data? The data type id, is used to store a reference to that object.

This data type is used normally, to pass object’s to methods in Objective C. I’ll cover it in more detail in a later tutorial.

Arrays

You can store multiple value’s in just one array variable. Think of an array as a big box, with many little boxes inside. You could give an array the name ages, and then place inside of it, all the ages for everyone you know. You would create that array like this:

int ages[10]; // This creates an array with space for 10 integer’s

int ages[5] = { 1, 34, 35, 74, 54 }; /* This created an array named ages, that holds a maximum of 5 integers, and then assigns the five value’s to the array */

char letters[5] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ }; // Doing the same thing for a series of characters

If you wanted to use NSLog with this last array like this:

NSLog (@”The first letter is %c”, letters[1] );

Your output would be: The first letter is b. Why didn’t it output the letter a? The reason is that the first value stored in an array, is stored in the index 0, or letters[0].

You could also output the letters array as just one string of character’s like this:

NSLog (@”All the characters %s”, letters); // The output would be: All the characters abcde

You can also create multidimensional arrays. Let’s say you wanted to store this table in an array.

1 2 3 4 5

6 7 8 9 1

2 3 4 5 6

You would do that like this:

int numberTable [3] [5] = { {1, 2,3,4,5}, {6,7,8,9,1}, {2,3,4,5,6} };

The value of numberTable [1] [1], would be equal to 7. Remember the first index is 0, if your confused!

If you want to store a variable number of object’s in an array there is a way. Put the array inside of a method, or function like this:

int varArray (int n)

{

int myNumbers[n];

}

There are numerous function’s you can use to manipulate array’s, but I’ll leave that discussion for a later tutorial.

Strings

If you want to store a series of character’s to a variable you need to create a string variable. If you want to be able to use strings, you will have to include a new framework like you do for Foundation.h. You need to include this line of code:

#import <Foundation/NSString.h>

You now can create and assign values to a string variable like this:

NSString *str = @”This is a string”; // Note: Remember this string because I refer to it below!

You would output this string, using NSLog like this:

NSLog (@”%@”, str);

The code %@, is used to output a string, array, or object to the screen.

One problem with these type’s of strings, is that after you create them they cannot be changed. To create a string that can be changed you need to declare it with NSMutableString, like this:

NSMutableString *iCanChange; // Creates a string storage variable

iCanChange = [NSMutableString stringWithString: str]; /* Stores the string in the variable str, in the string iCanChange */

Here is the code needed to perform operation’s on strings using NSMutable:

[iCanChange setString: @”This is a string”];

/* Sets the value of the string named iCanChange */

[ iCanChange insertString: @” that has changed” atIndex: 16];

/* Changes the string in iCanChange to “This is a string that has changed”. I inserted the string “that has changed” after the 16th letter in the variable iCanChange */

int lengthOString = [iCanChange length];

/* Assigns the length of the string to an integer named lengthOString */

[iCanChange appendString: @” again”];

/* Tacks the string “ again” to the end of the string variable iCanChange */

[iCanChange deleteCharactersInRange: NSMakeRange (16, 33)];

/* Deletes both of the strings from the end of iCanChange, This function is deleting all the character’s starting from the 16th character till the 33rd. */

[[iCanChange deleteCharactersInRange: NSMakeRange(11,16) withString: @”a changeable string”];

/* This will replace the string characters that lie from index 11 to 16, with the string “a changeable string”. */

locationOChar = [iCanChange rangeOfString: @”string”];

/* Returns the index for the string named “string” */

If this is confusing to you, don’t worry, it will all make sense once I start making a bunch of sample programs.

That’s All Folks

Here I covered a whole bunch of different data types available to you with Objective C. Next I’ll cover looping and decision making. Don’t worry, you’ll completely grasp that.

If you have any questions leave them in the comment section below.

Here to Serve

Think Tank