Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Objective C Fundamentals

Objective-C Introduction

iOS development uses the Objective C language, which is an object-oriented language, so it is very simple for programmers who already know object-oriented languages.


Interface and Implementation

In Objective, a completed file is called an interface file, and the definition of such a file is called an implementation file.

A simple interface file MyClass.h is shown below:



@interface MyClass:NSObject{ 

// Class variable declaration

}

// Class property declaration

// Class methods and declarations

@end

The implementation MyClass.m file is as follows



@implementation MyClass

// Class method definition

@end


Creating objects

Creating an object is as follows



MyClass  *objectName = [[MyClass alloc]init] ;


Methods

A method declared in Objective C is shown below:



-(returnType)methodName:(typeName) variable1 :(typeName)variable2;

An example is shown below:



-(void)calculateAreaForRectangleWithLength:(CGfloat)length 

andBreadth:(CGfloat)breadth;



You may wonder what the andBreadth string is; in fact, its optional strings help us read and understand methods, especially when a method is called.

To call this method within the same class, we use the following statement.



[self calculateAreaForRectangleWithLength:30 andBreadth:20];

As mentioned above, the use of andBreath helps us understand that breadth is 20. Self is used to specify that it is a class method.

Class Methods

Class methods can be accessed directly without creating an object. They do not have any variables or associated objects. The example is as follows:



+(void)simpleClassMethod;

It can be accessed by using the class name (assume MyClass as the class name), as shown below:



[MyClass simpleClassMethod];

Instance Methods

Only instance methods can be accessed after creating an object of a class, with instance variables allocated in memory. An instance method is as follows:



-(void)simpleInstanceMethod; 

After creating an object of the class, it can be accessed, as shown below:



MyClass  *objectName = [[MyClass alloc]init] ;

[objectName simpleInstanceMethod];


Important Data Types in Objective C

No. Data Type
1 NSString Strings
2 CGFloat The basic type for floating-point values
3 NSInteger Integer type
4 BOOL Boolean type

Printing Logs

NSLog is used to print a statement; it prints to the device log and to the console in debug builds.

For example, NSlog(@"")


Control structures

Apart from a few additions, most control structures are the same as in C and C++

Properties

Attributes used to access variables of a class from outside the class

For example: @property (nonatomic, strong) NSString *myString

Accessing Attributes

Properties can be accessed using the dot operator; to access the above property, do the following



self.myString = @"Test";

You can also use the set method, as shown below:



[self setMyString:@"Test"];

Categories

Categories are used to add methods to an existing class. In this way, methods can be added to a class even without the implementation file in which the actual class is defined. A sample category for MyClass is shown below:



@interface MyClass(customAdditions)

- (void)sampleCategoryMethod;

@end



@implementation MyClass(categoryAdditions)



-(void)sampleCategoryMethod{

   NSLog(@"Just a test category");

}


arrays

NSMutableArray and NSArray are array classes used in ObjectiveC; the former is a mutable array and the latter is an immutable array, as follows:



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

[anArray addObject:@"firstobject"];

NSArray *aImmutableArray = [[NSArray alloc]

initWithObjects:@"firstObject",nil];


Dictionaries

NSMutableDictionary and NSDictionary are dictionaries used in Objective; the former is a mutable dictionary and the latter is an immutable dictionary, as shown below:



NSMutableDictionary*aMutableDictionary = [[NSMutableArray alloc]init];

[aMutableDictionary setObject:@"firstobject" forKey:@"aKey"];

NSDictionary*aImmutableDictionary= [[NSDictionary alloc]initWithObjects:[NSArray arrayWithObjects:

@"firstObject",nil] forKeys:[ NSArray arrayWithObjects:@"aKey"]];