Objective-C has two categories of data types. First, remember that Objective-C is asuperset 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 datatypes." Second, Objective-Cprovides several high-level data structures like strings, arrays, dictionaries,and dates.
Basic Data Types: Some of the morecommon data types we use in Objective-C include:
· int – An integer value, i.e. a whole number (nodecimals) that includes zero and negative numbers.
· float – A floating point value that includes asmany decimal places as it can hold. Because the decimal place can change, orfloat, its important to know that these values may technically be imprecise.When precise decimals are needed, like for currency, we should use theNSDecimalNumber 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, thefollowing 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 Aor 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 charactersstrung 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 typesmentioned above (among others).
Variables:a variable is basically a container used to hold data. When we create avariable 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 computerare allocated as the “container” to hold the data, and we store a reference tothat container with a name that we provide.
|
1 |
2 |
3 |
4 |
5 |
|
int |
favoriteNumber |
= |
24 |
; |
|
NSString |
*title |
= |
@”Objective-C” |
; |
|
|
A |
|
B |
|
Samepattern.
Variabledeclaration in other prog.lang.
A. The asterisk (*) can be a little confusing. This is not part of thevariable name. It can be placed anywhere between the data type and the variablename, so the following are all equivalent:
NSString* title;
NSString * title;
NSString *title;
The * symbolis actually an operator that is used to de-reference a pointer. Pointers arepretty much what they sound like. They “point” to a location in memory wherethe actual data is stored. That location in memory is the true container thatholds the data. Pointers are a part of regular C and are used because they aremore efficient. When we use pointers in our programs, we only need to store andcopy 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 veryquickly run into problems of not having enough memory. For example, it’s muchmore efficient to simply point to the location for a large video file and usethat pointer multiple times in code than to actually have to use all the dataof that large video file every time we access it in code.
Okay, soback to the asterisk: what does it mean to de-reference a pointer? It simplymeans that we obtain the value stored in the memory where the pointer ispointing to.
B. The “at” symbol (@) plus text insidedouble quotes make up an “NSString literal”. As someone who used Strings inother programming languages first, this will be confusing.
The @ symbolis used in a few places in Objective-C, and basically it’s a way to signal thatwhatever 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 objectsare different than the counterparts used in C, so that’s why the @ symbolappears before the first double quote.
Object Creation:
· +alloc: Class method of NSObject. Returns a new instance of the receiving
class.
Ex: [Student alloc]
· -init: Instance method of NSObject. Implemented by subclasses to initialize a
newobject (the receiver) immediately after memory for it has been allocated.
Ex: [Student init]
· +new: 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];
}
So toconclude we can say that
alloc goes with init
new = alloc + init
· -release: Instance method of NSObject delegate. Decrements the receiver’s
referencecount.
NSMutableArray*stuff = [[NSMutableArray alloc] init];
// use the array
[stuff release];
release thearray immediately, which in memory-starved environments can be
useful.
· -autorelease: Instance method of NSObject delegate. All of the autoreleased
objects aredestroyed after the program is done executing.
+ (CarStore*)carStore {
CarStore *newStore = [[CarStore alloc]init];
return [newStore autorelease];
}
· -Retain :Instance method of NSObject delegate. Increments the receiver’s
referencecount.
· -Copy:Instance method of NSObject delegate. Returns a new instance that’s a
copy ofthe receiver.
· Dealloc: This method is called automatically by the runtime—you should never
try tocall dealloc yourself.
· Object creation examples:
(1) One-line declaration, allocation,initialization.
Student *myStudent =[[Student alloc] init];
(2) Multi-line declaration, allocation,initialization.
Student *myStudent;
myStudent = [Studentalloc];
myStudent =[myStudent init];
Next, we will learn about : Objective C : object creation method explanations
Leave a Comment