【Objective-C】Objective-C summary

method definition

Reference: https://www.yiibai.com/objective_c/objective_c_functions.html

The general form of method definition in Objective-C programming language is as follows

- (return_type) method_name:( argumentType1 )argumentName1 
    joiningArgument2:( argumentType2 )argumentName2 ... 
    joiningArgumentn:( argumentTypen )argumentNamen {
    
    
    body of the function
}

Example:

/* 返回两个参数的最大值 */
- (int) max:(int) num1 secondNumber:(int) num2 {
    
    

   /* 局部变量声明 */
   int result;

   if (num1 > num2) {
    
    
      result = num1;
   } else {
    
    
      result = num2;
   }

   return result; 
}

alloc method

This line of code is often used in OC NSObject *object = [[NSObject alloc] init]; to create an object

Through the analysis of the underlying source code of alloc, we can learn that:
① The main purpose of alloc is to open up memory space;
② The main core logic It is to calculate the memory size->Apply for memory space->Bind isa;
③ The calculated memory size is aligned according to 16 bytes.

Reference: https://www.cnblogs.com/mysweetAngleBaby/p/16747295.html

Guess you like

Origin blog.csdn.net/qq_39441603/article/details/134296627