Objective-C Introduction
Objective-C language is used in iOS development. It is an object-oriented language, so it is very simple for programmers who have already mastered object-oriented language knowledge.
Interface and Implementation
In Objective-C, the files completed are called interface files, and the definitions of such files are called implementation files.
A simple interface file MyClass.h is shown in the figure below:
@interface MyClass:NSObject{
// 类变量声明
}
// 类属性声明
// 类方法和声明
@end
Execute the MyClass.m file, as shown below:
@implementation MyClass // 类方法定义 @end
Creating Objects
Complete creating the object, as shown below:
MyClass *objectName = [[MyClass alloc]init] ;
Methods
Methods declared in Objective-C are shown below:
-(returnType)methodName:(typeName) variable1 :(typeName)variable2;
An example is shown below:
-(void)calculateAreaForRectangleWithLength:(CGfloat)length andBreadth:(CGfloat)breadth;
You might wonder what the andBreadth string is. In fact, its optional string can help us read and understand methods, especially when the method is called.
To call this method in the same class, we use the following statement.
[self calculateAreaForRectangleWithLength:30 andBreadth:20];
As mentioned above, the use of andBreath helps us understand that breath is 20. Self is used to specify that it is a method of the class.
Class MethodsClass methods can be accessed directly without creating an object. They do not have any variables associated with the object. Examples are as follows:
+(void)simpleClassMethod;
It can be accessed by using the class name (assuming MyClass as the class name), as shown below:
[MyClass simpleClassMethod];Instance Methods
Instance methods can only be accessed after creating an object of the class, with memory allocated to the instance variables. Instance methods are shown below:
-(void)simpleInstanceMethod;
After creating an object of the class, it can access it. As shown below:
MyClass *objectName = [[MyClass alloc]init] ; [objectName simpleInstanceMethod];
Important Data Types in Objective C
| Serial Number | Data Type |
|---|---|
| 1 | NSString string |
| 2 | CGFloat, the basic type for floating-point values |
| 3 | NSInteger, the integer type |
| 4 | BOOL, the Boolean type |
Printing Logs
NSLog is used to print a statement, which will be printed in the device log and on the console of the debug version and in debug mode respectively.
For example, NSLog(@"");
Control Structures
Apart from a few added clauses, most of the control structures are the same as those in C and C++.
Properties
Used to access the variable properties 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 above property, you can 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 existing classes. In this way, methods can be added to a class even without an implementation file in which the actual class is defined. A sample category of 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 the array classes used in Objective-C. 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 the dictionary classes used in Objective-C. 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"]];Other Extensions