Objective-C basics
Objective-C Introduction
In the development of iOS, Objective C language is used, which is an object-oriented language, so for those who have mastered object-oriented language It is very simple for programmers with knowledge.
Interface and implementation
The files completed in Objective are called interface files, and the definition of this type of file is called an implementation file.
A simple interface file MyClass.h will be as shown in the figure:
@interface MyClass:NSObject{ // 类变量声明}// 类属性声明// 类方法和声明@end
Execute the MyClass.m file as shown below
@implementation MyClass// 类方法定义@end
Create objects
Complete creating the object, as shown below
MyClass *objectName = [[MyClass alloc]init] ;
Methods (methods)
The methods declared in Objective C are as follows:
-(returnType)methodName:(typeName) variable1 :(typeName)variable2;
The following is displayed Here is an example:
-(void)calculateAreaForRectangleWithLength:(CGfloat)length andBreadth:(CGfloat)breadth;
You may think what is andBreadth string, in fact its optional string can help us read and understand the method, 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 a class.
Class methods (class methods)Class methods can be accessed directly without creating an object. They don't have any variables and objects associated with them. An example is as follows:
+(void)simpleClassMethod;
It can be accessed by using the class name (assumed as MyClass class name) as follows:
[MyClass simpleClassMethod];Instance methods
Objects of the class that can be created After accessing only instance methods, memory is allocated to instance variables. The instance methods are as follows:
-(void)simpleInstanceMethod;
After creating an object of a class, it can access it. As shown below:
MyClass *objectName = [[MyClass alloc]init] ;[objectName simpleInstanceMethod];
Important data types of Objective C
Data types | |
---|---|
NSString string | |
CGfloat The basic type of floating point value | |
NSInteger Integer type | |
BOOL Boolean type |