【Objective-C】Objective-C汇总

方法定义

参考:https://www.yiibai.com/objective_c/objective_c_functions.html

Objective-C编程语言中方法定义的一般形式如下

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

示例:

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

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

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

   return result; 
}

alloc 方法

OC中经常使用 NSObject *object = [[NSObject alloc] init]; 这行代码去创建一个对象

通过对alloc底层源码的分析, 可以了解到:
① alloc的主要目的是开辟内存空间;
② 主要的核心逻辑是 计算内存大小->申请内存空间->绑定isa;
③ 计算内存大小是按照16字节对齐的。

参考:https://www.cnblogs.com/mysweetAngleBaby/p/16747295.html

猜你喜欢

转载自blog.csdn.net/qq_39441603/article/details/134296627