Introduction to Objective-C
Objective-C is the language used in iOS development. It is an object-oriented language, making it straightforward for programmers who already have knowledge of object-oriented languages.
Interface and Implementation
Files completed in Objective-C are called interface files, and the definitions of these files are called implementation files.
A simple interface file MyClass.h will look like this:
@interface MyClass:NSObject{
// Class variable declarations
}
// Class property declarations
// Class method and declarations
@end
The implementation file MyClass.m is shown below:
@implementation MyClass
// Class method definitions
@end
Creating Objects
Objects are created as follows:
MyClass *objectName = [[MyClass alloc]init];
Methods
Methods in Objective-C are declared as follows:
-(returnType)methodName:(typeName) variable1 :(typeName)variable2;
An example is shown below:
-(void)calculateAreaForRectangleWithLength:(CGfloat)length
andBreadth:(CGfloat)breadth;
You might wonder what "andBreadth" is; it's an optional string that helps us read and understand the method, especially when it's called.
To call this method within the same class, we use the following statement:
[self calculateAreaForRectangleWithLength:30 andBreadth:20];
As mentioned earlier, "andBreadth" helps us understand that breadth is 20. "Self" is used to specify that it's a class method.
Class methods can be accessed directly without creating an object. They have no variables associated with the object. An example is:
+(void)simpleClassMethod;
It can be accessed using the class name (assuming MyClass as the class name) as follows:
[MyClass simpleClassMethod];
Instance methods can only be accessed after creating an object of the class, where memory is allocated for instance variables. An instance method looks like this:
-(void)simpleInstanceMethod;
After creating an object of the class, it can be accessed as follows:
MyClass *objectName = [[MyClass alloc]init];
[objectName simpleInstanceMethod];
Important Data Types in Objective-C
Number | Data Type |
---|---|
1 | NSString string |
2 | CGfloat basic floating-point value |
3 | NSInteger integer type |
4 | BOOL boolean type |
Printing Logs
NSLog is used to print a statement, which will be printed in the device log and the console in debug mode.
For example:
NSLog(@"");
Control Structures
Most control structures are the same as in C and C++, with a few additional clauses.
Properties
Properties are used to access variables of a class from outside the class.
For example:
@property (nonatomic, strong) NSString* myString
Accessing Properties
Properties can be accessed using the dot operator. To access the previous property, you can do the following:
self.myString = @"Test";
Alternatively, you can use the set method as follows:
[self setMyString:@"Test"];
Categories
Categories are used to add methods to existing classes. This way, you can add methods to a class without modifying its implementation file. A sample category for MyClass looks like this:
@interface MyClass(customAdditions)
- (void)sampleCategoryMethod;
@end
@implementation MyClass(categoryAdditions)
-(void)sampleCategoryMethod{
NSLog(@"Just a test category");
}
Arrays
NSMutableArray and NSArray are the array classes used in Objective-C, where the former is a mutable array and the latter is an immutable array. Examples are:
NSMutableArray *aMutableArray = [[NSMutableArray alloc]init];
[anArray addObject:@"firstobject"];
NSArray *aImmutableArray = [[NSArray alloc]
initWithObjects:@"firstObject", nil];
Dictionary
NSMutableDictionary and NSDictionary are dictionaries used in Objective-C, where the former is a mutable dictionary and the latter is an immutable dictionary, as shown below:
NSMutableDictionary *aMutableDictionary = [[NSMutableDictionary alloc] init];
[aMutableDictionary setObject:@"firstobject" forKey:@"aKey"];
NSDictionary *aImmutableDictionary = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:
@"firstObject", nil] forKeys:[NSArray arrayWithObjects:@"aKey", nil]];