自动释放池Demo1
#import <Foundation/Foundation.h>
#import "XiaoYuRen.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 获取自动释放池对象
NSAutoreleasePool *poolR = [[NSAutoreleasePool alloc] init];
// 获取小鱼人对象
XiaoYuRen *yuRen = [[XiaoYuRen alloc] init];
NSLog(@"%ld",[yuRen retainCount]);
[yuRen retain];
NSLog(@"%ld",[yuRen retainCount]);
// 把对像放进缓存池
// 进行延时释放,对象的引用计数不会立马减一,只有当缓存池销毁的时候里面的对象引用计数才会减一
[yuRen autorelease];
NSLog(@"%ld",[yuRen retainCount]);
[poolR release];
NSLog(@"%ld",[yuRen retainCount]);
// 将对象就近放入缓存池
XiaoYuRen *xiaoYuRen = [[[XiaoYuRen alloc] init] autorelease];
XiaoYuRen *xiaoYu;
@autoreleasepool {
// 放进缓存池
xiaoYu = [[[XiaoYuRen alloc] init] autorelease];
NSLog(@"%ld",[xiaoYu retainCount]);
// [xiaoYu autorelease];
}
// 如果对象已经放入缓存池,且对象的引用计数为1,不能再次release或者autorelease 如果再次release 则会出现两次release
[xiaoYu autorelease];
NSLog(@"%ld",[xiaoYu retainCount]);
}
return 0;
}
#import <Foundation/Foundation.h>
@interface XiaoYuRen : NSObject
@end
#import "XiaoYuRen.h"
@implementation XiaoYuRen
@end
自动释放池Demo3
#import <Foundation/Foundation.h>
#import "DaZui.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
DaZui *shuQi = [DaZui defaultHero];
shuQi.name = @"寻龙诀";
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Hero : NSObject
// 年龄
@property (nonatomic,assign)NSInteger heroAge;
+ (instancetype)defaultHero;
@end
#import "Hero.h"
#import "DaZui.h"
@implementation Hero
+ (instancetype)defaultHero
{
Hero *yaoChen = [[[Hero alloc] init] autorelease];
return yaoChen;
}
- (void)dealloc
{
}
@end
#import "Hero.h"
@interface DaZui : Hero
@property (nonatomic,copy)NSString *name;
@end
#import "DaZui.h"
@implementation DaZui
@end
ARCDemo1
#import <Foundation/Foundation.h>
#import "Ticket.h"
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 1. 对象没有指针指向的时候会被销毁
//获取一张票
// 强引用
// Ticket *ticket = [[Ticket alloc] init];
// ticket = NULL;
// 2. 对象有指针指向的时候不会被销毁
// Ticket *ticketOne = [[Ticket alloc] init];
// Person *person = [[Person alloc] init];
//
// // 人拿票
// person.ticket = ticketOne;
//
// 3.弱引用
// 系统在计算一个对象的引用个数时,会忽略__weak修饰的指针
// 对象刚创建出来就会被销毁
__weak Ticket *ticketTwo = [[Ticket alloc] init];
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Ticket : NSObject
@end
#import "Ticket.h"
@implementation Ticket
- (void)dealloc
{
// ARC环境下,不要去调用[super dealloc]
// [super dealloc];
NSLog(@"票被抢走了");
}
@end
#import <Foundation/Foundation.h>
#import "Ticket.h"
@interface Person : NSObject
@property(nonatomic, retain)Ticket *ticket;
@end
#import "Person.h"
@implementation Person
@end
解决ARC的局限
#import <Foundation/Foundation.h>
#import "Cat.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Cat *cat = [[Cat alloc] init];
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Cat : NSObject
@end
#import "Cat.h"
@implementation Cat
- (void)dealloc
{
NSLog(@"🐱 已死");
}
@end
解决ARC的局限Test
#import <Foundation/Foundation.h>
#import "Cat.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
__strong Cat *cat = [[Cat alloc] init];
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Cat : NSObject
@end
#import "Cat.h"
@implementation Cat
- (void)dealloc
{
NSLog(@"🐱 已死");
[super dealloc];
}
@end
强引用注意点
#import <Foundation/Foundation.h>
#import "Dog.h"
#import "SingleDog.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Dog *daHuang = [[Dog alloc] init];
SingleDog *wang = [[SingleDog alloc] init];
// 使用的时候要注意不要循环引用
daHuang.singleDog = wang;
wang.dog = daHuang;
}
return 0;
}
#import <Foundation/Foundation.h>
//#import "Dog.h"
@class Dog;
@interface SingleDog : NSObject
@property (nonatomic, retain)Dog *dog;
@end
#import "SingleDog.h"
@implementation SingleDog
@end
#import <Foundation/Foundation.h>
#import "SingleDog.h"
@interface Dog : NSObject
@property (nonatomic, retain)SingleDog *singleDog;
@end
#import "Dog.h"
@implementation Dog
@end