OC内存管理

2018-09-21  本文已影响0人  VampireJune

什么是 内存管理

:存储 OC对象
:存储 非OC对象 (栈内存会被系统自动回收)

对象 的基本结构

引用计数器 的作用

引用计数器的操作

对象 的销毁

@implementation VampireJune
// 当一个 VampireJune 对象被回收的时候,就会自动调用这个方法
- (void)dealloc
{
  NSLog(@"VampireJune 对象被回收");

  // super 的 dealloc 一定要调用,而且放在此方法的最后面
  [super dealloc];
}
@end

int main()
{
  VampireJune *v = [[VampireJune alloc] init];
  NSUInteger c = [v retainCount];
  NSLog(@"v的计数器:%ld ",c );
  
  // retain 方法 返回的是对象本身
  [v retain];

  [v release];

  // 指针 v 变成空指针
 v = nil;

  return 0;
}

内存管理原则

多对象内存管理

总结

内存管理代码规范

 - (void)setAge:(int)age
  {
  // 基本数据类型不需要内存管理
  _age = age;
  }

2> OC对象类型

  - (void)setVampire:(Vampire *)vam
  {
    // 1.先判断是不是新传进来的对象
    if( vam != _vam)
    {
        // 2.对旧对象做一次 release
        [_vam release];

        // 3.对新对象做一次 retain
        _vam = [vam retain];
    }
  }
 - (void)dealloc
  {
    [_vam release];
    [super dealloc];
  }

@property 的参数

retain : 生成的 set 方法里面, release 旧值,retain 新值
@property (retain) Book *book;
@property (nonatomic, retain, readwrite) Book *book;
@property (nonatomic, assign, readwrite) int num;
@interface VampireJune : NSObject
// setter 方法的 冒号一定不能少!!!
@property (getter = abc, setter = setAbc:) int age;
@end
int main ()
{
  VampireJune *v = [[VampireJune alloc] init];
  // setter 使用
  v.abc = 10;
  [v setAbc: 10];

   // getter 使用
  NSLog(@"p的age:%ld   %ld ",[v abc], v.abc);
}

@property 参数补充

// 不能为nil
@property (nonatomic, strong, nonnull) NSArray *names;
等于
@property (nonatomic, strong) NSArray * __nonnull names;

// 可以为nil,默认情况下,不加 nullable,setter 和 getter 都是可以为 nil
// nullable 更多的作用是在于程序员之间的沟通交流(提醒同事某个属性可能是 nil)
@property (nonatomic, strong, nullable) NSArray *names;
等于
@property (nonatomic, strong) NSArray * __nullable names;

null_resettable : setter 可以为 nil,getter 不可以为 nil
@property (null_resettable, nonatomic, strong) NSArray *names;

@class 和 #import 的区别

#import "B.h"
@interface A : NSObject{
  B *b;
}
@end

#import "A.h"
@interface B : NSObject{
  A *a;
}
@end

#import "B.h"
@interface A : NSObject{
  @property (nonatomic, retain) B *b;
}
@end

@class A.h
@interface B : NSObject{
  @property (nonatomic, assign) A *a;
}
@end

autorelease

 @autoreleasepool
 { // { 开始代表创建释放池
   VampireJune *v = [[[VampireJune alloc] init] autorelease];
 } // } 结束代表销毁释放池
@autoreleasepool
  { 
    // 1
    VampireJune *v = [[[VampireJune alloc] init] autorelease];

    // 0
    [v release]
  }
 @autoreleasepool
{ 
    VampireJune *v = [[[[VampireJune alloc] init] autorelease] autorelease];
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

 [pool release]; // [pool drain];(Mac开发 上使用的)
@autoreleasepool
{ 

}
+ (id)VampireJune
{
  return [[[self alloc] init] autorelease];
}

MRC 手动管理内存

ARC 自动引用计数 是 编译器特性

// 错误的写法,没有意义的写法
__weak VampireJune *v = [[VampireJune alloc] init];

循环引用

@class B.h
@interface A : NSObject{
  @property (nonatomic, strong) B *b;
}
@end

@class A.h
@interface B : NSObject{
  @property (nonatomic, weak) A *a;
}
@end

上一篇 下一篇

猜你喜欢

热点阅读