Introduction to Objective C

13
Introduction to Objective C Hassan Aftab Introduction of Objective C for beginners

Transcript of Introduction to Objective C

Page 1: Introduction to Objective C

Introduction to Objective C

Hassan Aftab

Introduction of Objective C for beginners

Page 2: Introduction to Objective C

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

Page 3: Introduction to Objective C

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.

Page 4: Introduction to Objective C

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

Page 5: Introduction to Objective 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

Page 6: Introduction to 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.

Page 7: Introduction to Objective C

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.

Page 8: Introduction to Objective C

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.

Page 9: Introduction to Objective C

Collections

These datatypes are used to store large amount of data.

These datatypes include NSArray NSDictionary NSSet

Page 10: Introduction to Objective C

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

Page 11: Introduction to Objective C

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

Page 12: Introduction to Objective C

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

Page 13: Introduction to Objective C

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