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

##Serial numberData types1NSString string2CGfloat The basic type of floating point value3NSInteger Integer type4BOOL Boolean type

Print Log

NSLog is used to print a statement, which will be printed in the device log and on the console of the debug version and debug mode respectively.

Such as NSlog(@"");


Control structure

Except for a few additional clauses, most of the control structures are the same as C and C

Properties

Used to access variable properties of external classes of the class

For example: @property (non-atomic, strong) NSString*myString

Access properties

You can use the dot operator to access properties. To access the previous property, you can perform the following operations

self.myString = @"Test";

You can also use set Methods, as follows:

[self setMyString:@"Test"];

Categories

classes are used to add methods to existing classes. This way you can add methods to a class without even executing the file in which you define the actual class. The sample categories of MyClass are as follows:

@interface MyClass(customAdditions)- (void)sampleCategoryMethod;@end@implementation MyClass(categoryAdditions)-(void)sampleCategoryMethod{   NSLog(@"Just a test category");}

Arrays

NSMutable and NSArray are array classes used in ObjectiveC. The former is a variable array and the latter is not Variable array. As follows:

NSMutableArray *aMutableArray = [[NSMutableArray alloc]init];[anArray addObject:@"firstobject"];NSArray *aImmutableArray = [[NSArray alloc]initWithObjects:@"firstObject",nil];

Dictionary

NSMutableDictionary and NSDictionary are dictionaries used in Objective. The former is a variable 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"]];