Introduction to Objective C
-
Upload
hassan-aftab -
Category
Software
-
view
94 -
download
1
Transcript of Introduction to Objective C
Introduction to Objective C
Hassan Aftab
Introduction of Objective C for beginners
Overview
These slides are for beginners who want to learn Objective C for the development of iOS.
Following topics are covered in these slides
− What is Objective C?
− Basic Datatypes
Primitive datatypes {int, float, double... } Object types {NSString, NSNumber, NSInteger...} Collections {NSArray, NSDictionary, NSSets...}
What is Objective C?
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.
It is the main programming language used by Apple for the OS X and iOS operating systems.
To use the Objective C, Apple provides you Xcode, which can run only on OSX.
Basic Datatypes
As you must already be aware, you need datatypes to store different types of data.
Objective-C also provides you the basic datatypes provided by other programming languages
− int
− float
− double
− char
etc...
These datatypes can be used in Objective C according to requirements.
These datatypes will be used in the same way they are used in C\C++.
Object Datatypes
These datatypes are basically the objects of different classes provided by Objective C.
These datatypes include− NSNumber− NSString− NSInteger
NS is the prefix you will find with most of the classes provided by Objective - C
NSNumber
NSNumber is the class used to store any type of number, currency, percentage etc.
It is defined in following ways
− NSNumber * Num = [[NSNumber alloc]initWithInt:2];
Here we are defining the pointer “ * Num ” of class NSNumber and allocate it some memory using alloc. Then we initialize is with Integer value “2”.
− NSNumber * Num = @(2); This is the shortcut to do same above task.
NSInteger
NSInteger is datatype used to store integer or long values depending on your processor. If processor is x86, it will store int value, otherwise if processor is x64 it will store long value.
It is defined in following ways
− NSInteger Num = 2 No need to define a pointer for this
datatype because it is just another name for these premetive datatypes.
NSString
NSNumber is the class used to store any type of number, currency, percentage etc.
It is defined in following ways
− NSString * string = [NSString stringWithFormat:@"Hassan"];
Here we are defining the pointer “ * string ” of class NSString and assign it with value “Hassan”.
− NSString * string = @"HASSAN"; This is the shortcut to do same above task.
Collections
These datatypes are used to store large amount of data.
These datatypes include NSArray NSDictionary NSSet
NSArray
NSArray stores the collection of data. Data can be of any type. It can store different types of data at the same time.
It can be defined as.
NSArray * justArray = [([NSArray alloc])initWithObjects:@(12), @"Hassan", nil];
The above statement will declare a NSArray and initialize it with objects: @(12) is object type NSNumber and @“Hassan” is the object type NSString
NSDictionary
NSDictionary is a collection that can hold key value pairs of data. Both values should be objects.
It can be defined as: NSDictionary *dict = [[NSDictionary
alloc]initWithObjectsAndKeys:@{@"Hassan": @"Aftab"},@{@(32): @"objects"}, nil];
Here we are defining the pointer “ * dict ” of class NSDictionary and initialize it with objects and keys. In key value pairs
NSSet
NSSet is a collection that can store data just like array. Only the data in the NSSet should be unique values.
It can be defined as:
• NSSet *set = [[NSSet alloc]initWithObjects:@(1),@(2), nil];
Here we are defining the pointer “ * set ” of class NSSet and initialize it with objects @(1), @(2).
Collections You must have noticed that we are placing “nil” at the
end of every collection. “nil” represents the end of data in the collection.
You can not remove or update any data in any of these collections.
If you want to use a collection where you are able to update the data then you should use:
NSMutableArray NSMutableDictionary NSMutableSet
Note: You can only store objects in these collections not primitive datatypes