Objective C :
Data Types, Variables, Object Creation
Objective-C has two categories of data types. First, remember that Objective-C is a
superset of C, so you have access to all of the native C data types like char,
int, float, etc. Objective-C also defines a few of its own low-level types,
including a Boolean type. Let's call all of these "primitive data
types." Second, Objective-C
provides several high-level data structures like strings, arrays, dictionaries,
Basic Data Types: Some of the more
common data types we use in Objective-C include:
· int – An integer value, i.e. a whole number (no
decimals) that includes zero and negative numbers.
· float – A floating point value that includes as
many decimal places as it can hold. Because the decimal place can change, or
float, its important to know that these values may technically be imprecise.
When precise decimals are needed, like for currency, we should use the
NSDecimalNumber data type.
· BOOL – Short for “boolean”, this is a 1-bit
“true” or “false” value that can only be in one of those states. The C language
(and hence, Objective-C) treat 0 as “false” and 1 as “true”. As such, the
following keywords can be used to represent true/false values: YES/NO,
TRUE/FALSE, true/false, 1,0.
· char – A single character, such as the letter A
or the symbol “#”. Note that lowercase and uppercase characters are different,
so “a” and “A” are two different characters.
· NSString – String data is a bunch of characters
strung together to make text, like a banner strung up at a party.
· NSNumber – This class is a lightweight “wrapper”
class that gives object-oriented features to the primitive number types
mentioned above (among others).
a variable is basically a container used to hold data. When we create a
variable in Objective-C, we usually specify what kind of data it will hold.
This is known as statically typing the data. Blocks of memory in the computer
are allocated as the “container” to hold the data, and we store a reference to
that container with a name that we provide.
declaration in other prog.lang.
declaration in Objective-C.
A. The asterisk (*) can be a little confusing. This is not part of the
variable name. It can be placed anywhere between the data type and the variable
name, so the following are all equivalent:
NSString * title;
The * symbol
is actually an operator that is used to de-reference a pointer. Pointers are
pretty much what they sound like. They “point” to a location in memory where
the actual data is stored. That location in memory is the true container that
holds the data. Pointers are a part of regular C and are used because they are
more efficient. When we use pointers in our programs, we only need to store and
copy a simple pointer, which is really just an address for a space in memory.
If we instead had to store and copy the data being pointed to, we might very
quickly run into problems of not having enough memory. For example, it’s much
more efficient to simply point to the location for a large video file and use
that pointer multiple times in code than to actually have to use all the data
of that large video file every time we access it in code.
back to the asterisk: what does it mean to de-reference a pointer? It simply
means that we obtain the value stored in the memory where the pointer is
B. The “at” symbol (@) plus text inside
double quotes make up an “NSString literal”. As someone who used Strings in
other programming languages first, this will be confusing.
The @ symbol
is used in a few places in Objective-C, and basically it’s a way to signal that
whatever it’s attached to is special to Objective-C and not part of regular C.
This is important when the computer compiles Objective-C code. NSString objects
are different than the counterparts used in C, so that’s why the @ symbol
appears before the first double quote.
: Class method of NSObject. Returns a new instance of the receiving class.
Ex: [Student alloc]
: Instance method of NSObject. Implemented by subclasses to initialize a new
object (the receiver) immediately after memory for it has been allocated.
Ex: [Student init]
: Class method of NSObject. Allocates a new instance of the receiving class,
sends it an init message, and returns the initialized object.
+new is implemented quite literally as:
+ (id) new
return [[self alloc] init];
conclude we can say that
alloc goes with init
new = alloc + init
: Instance method of NSObject delegate. Decrements the receiver’s reference
*stuff = [[NSMutableArray alloc] init];
// use the array
array immediately, which in memory-starved environments can be useful.
: Instance method of NSObject delegate. All of the autoreleased objects are
destroyed after the program is done executing.
CarStore *newStore = [[CarStore alloc]
return [newStore autorelease];
Instance method of NSObject delegate. Increments the receiver’s reference
Instance method of NSObject delegate. Returns a new instance that’s a copy of
: This method is called automatically by the runtime—you should never try to
call dealloc yourself.
Object creation examples:
One-line declaration, allocation,
Student *myStudent =
[[Student alloc] init];
Multi-line declaration, allocation,
myStudent = [Student
Next, we will learn about : Objective C : object creation method explanations