articles

Home / DeveloperSection / Articles / Objective C : Data Types, Variables, Object creation

Objective C : Data Types, Variables, Object creation

Tarun Kumar2707 04-Jul-2015

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, and dates.

 

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).

 

Variables: 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.


1

2

3

4

5

int

favoriteNumber

=

24

;

NSString

*title

=

@”Objective-C”

;

 

A

 

B

 

Same pattern.

Variable declaration in other prog.lang.

Variable 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;

      NSString * title;

      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.


Okay, so 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 pointing to.


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. 


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

new object (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 to conclude we can say that

alloc goes with init

new = alloc + init 

·   -release : Instance method of NSObject delegate. Decrements the receiver’s

reference count.


NSMutableArray *stuff = [[NSMutableArray alloc] init];

// use the array

[stuff release];

release the array immediately, which in memory-starved environments can be

useful. 

·   -autorelease : Instance method of NSObject delegate. All of the autoreleased

objects are destroyed 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

reference count. 

·   -Copy: Instance method of NSObject delegate. Returns a new instance that’s a

copy of the receiver. 

·    Dealloc : This method is called automatically by the runtime—you should never

try to call 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 = [Student alloc];


myStudent = [myStudent init];

 

Next, we will learn about : Objective C : object creation method explanations


Updated 07-Dec-2017

Leave Comment

Comments

Liked By