01-autorelease基本概念
Person.m
1 #import "Person.h" 2 3 @implementation Person 4 5 - (void)dealloc 6 { 7 [super dealloc]; 8 NSLog(@"Person 被销毁了"); 9 } 10 11 @end
Person.m
Person.h
1 #import <Foundation/Foundation.h> 2 3 @interface Person : NSObject 4 5 @end
person.h
main.m
1 //内存管理原则(配对原则):只要出现了new,alloc,retain方法,就要配对出现release,autorelease 2 //对象存入到自动释放池中,当这个池子被销毁的时候他会对池子中所有的对象进行一次release操作 3 //怎么把对象存到池子中 4 #import <Foundation/Foundation.h> 5 #import "Person.h" 6 7 int main(int argc, const char * argv[]) 8 { 9 /* 示例1 10 //自动释放池 11 //大括号代表池子的作用域 12 @autoreleasepool 13 { 14 15 //release 功能 retaincount - 1 16 //autorelease 好像功能也是 retaincount - 1 ? 17 //1 18 Person * p = [[Person alloc] init]; 19 // [p release];//retainCount立即 -1 20 [p autorelease]; //autorelease方法的作用只是将对象放入到池子中,然后返回一个self 21 22 NSLog(@"asdfasdf"); 23 24 }//代表,池子将要被销毁,对池子中所有的对象进行一次release操作,[p release] 0 25 */ 26 27 /* 28 //autoreleasepool我么可以自己随意的创建 29 30 // 示例2 31 //1//不管你这个对象时在@autoreleasepool 之内创建的还是在之外创建的,只要你在池子中调用了autorelease那么这个对象就会被放入到池子中 32 Person * p = [[Person alloc ] init]; 33 34 @autoreleasepool { 35 36 [p autorelease]; 37 38 }//p 0 39 */ 40 // 示例3 41 /* 42 //1 只有在自动释放池的作用域中调用对象的autorelease方法才能够正确的讲对象放入到池子中 43 Person * p = [[Person alloc] init]; 44 [p autorelease]; 45 46 NSLog(@"aaaa"); 47 @autoreleasepool { 48 49 } 50 NSLog(@"bbb"); 51 */ 52 //示例4 53 /* 54 // 1 55 Person * p = [[Person alloc] init]; 56 57 @autoreleasepool { 58 59 [p autorelease];//加入第一次 60 61 [p autorelease];//加入第二次 62 63 NSLog(@"abc"); 64 }//[p release]0 [p release] 65 66 NSLog(@"cbd"); 67 */ 68 69 //示例5 70 /* 71 //1 72 Person * p = [[Person alloc] init]; 73 74 @autoreleasepool { 75 76 @autoreleasepool { 77 78 [p autorelease]; 79 }//?[p release] 0 80 81 } 82 */ 83 84 85 Person * p = [[Person alloc] init]; 86 87 @autoreleasepool { 88 89 [p autorelease]; 90 91 @autoreleasepool { 92 93 94 [p autorelease]; 95 }//[p release] 0 96 97 98 }//[p release]; 99 100 101 return 0; 102 }
main.m
02-autorelease应用
Chief.m
1 #import "Chief.h" 2 3 @implementation Chief 4 5 - (void)dealloc 6 { 7 [super dealloc]; 8 NSLog(@"厨师被回收了"); 9 } 10 11 + (Chief *)chief 12 { 13 /* 14 Chief * chi = nil; 15 16 chi = [[Chief alloc] init]; 17 18 // [chi release]; 19 [chi autorelease]; 20 */ 21 22 Chief * chi = [[self alloc] init]; 23 24 25 return [chi autorelease]; 26 } 27 - (Food *)chiefMakeFood 28 { 29 30 31 //如果在方法中创建一个对象,最好给我初始化为nil,防止出现野指针错误 32 // 1 33 Food * food = [[Food alloc] init]; 34 food.name = @"凑豆乳"; 35 // // 0 36 // [food release]; 37 [food autorelease]; 38 39 return food; 40 41 } 42 43 @end
Chief.m
Chief.h
1 #import <Foundation/Foundation.h> 2 #import "Food.h" 3 4 @interface Chief : NSObject 5 6 + (Chief *)chief; 7 - (Food *)chiefMakeFood; 8 9 @end
Chief.h
Food.m
1 #import "Food.h" 2 3 @implementation Food 4 - (void)dealloc 5 { 6 [super dealloc]; 7 [_name release]; 8 NSLog(@"Food被销毁了"); 9 } 10 11 @end
Food.m
Food.h
1 #import <Foundation/Foundation.h> 2 3 @interface Food : NSObject 4 5 @property(nonatomic,retain) NSString * name; 6 7 @end
Food.h
main.m
1 #import <Foundation/Foundation.h> 2 #import "Chief.h" 3 //autorelease笔试面试出现的几率比[release] 4 5 int main(int argc, const char * argv[]) 6 { 7 8 @autoreleasepool { 9 10 /* 11 Chief * chi = [[Chief alloc] init]; 12 Food * food = [chi makeFood];; 13 14 15 NSLog(@"%@",food.name);//[nil name]; 16 17 [chi release]; 18 */ 19 20 21 22 Chief * chi = [Chief chief]; 23 Food * food = [chi chiefMakeFood]; 24 25 NSLog(@"%@",food.name); 26 27 NSString * name = [NSString stringWithFormat:@"%@",@"施瓦辛格"];//都是autorelease 28 29 NSLog(@"%@",name); 30 31 } 32 return 0; 33 }
main.m
03-ARC机制
Person.m
1 #import "Person.h" 2 3 @implementation Person 4 5 - (void)dealloc 6 { 7 // [super dealloc];不能够在调用 8 // releae retain 在ARC机制中不能够在去手动调用 9 NSLog(@"Person 被释放了"); 10 } 11 12 @end
Person.m
Person.h
1 #import <Foundation/Foundation.h> 2 3 @interface Person : NSObject 4 5 @end
Person.h
main.m
1 //ARC简单,不用程序员在去管理内存 2 //1.强指针 Strong 3 //2.弱指针 weak 4 5 //只要有强指针指向一个对象,那么系统就不会回收该对象 6 //只要没有强指针指向对象,系统立即回收该对象 7 8 //弱指针不影响,对象被回收 9 10 //默认情况下,所有的指针都是强指针类型 11 12 #import <Foundation/Foundation.h> 13 #import "Person.h" 14 15 void test(Person * v)//Person * v = 16 { 17 v = nil; 18 }// 19 20 int main(int argc, const char * argv[]) 21 { 22 23 24 25 /* 26 27 Person * p = [[Person alloc] init]; 28 29 // p = nil; 30 31 Person * p1 = p; 32 33 p = nil; 34 */ 35 36 37 Person * p = [[Person alloc] init]; 38 39 test(p); 40 41 p = nil; 42 43 NSLog(@"adfasdf"); 44 /* 45 //创建出来就会立即被释放掉,应为没有强指针指向该对象 46 __weak Person * p = [[Person alloc] init]; 47 NSLog(@"adfadf"); 48 */ 49 50 /* 51 Person * p = [[Person alloc] init]; 52 53 __weak Person * p1 = p; 54 55 p = nil; 56 */ 57 58 59 60 return 0; 61 }
main.m
04-ARC机制下组合关系
person.m
1 #import "Person.h" 2 3 @implementation Person 4 5 -(void)dealloc 6 { 7 NSLog(@"person 被销毁了"); 8 } 9 10 @end
Person.m
Person.h
1 #import <Foundation/Foundation.h> 2 #import "Car.h" 3 4 @interface Person : NSObject 5 6 //@property(nonatomic,retain)Car * car; 7 //ARC机制 strong 对象,手动内存管理的retain关键字,(一定能够都是应用在对象类型变量上) 8 //ARC机制中的 weak 对象手动内存管理的assign关键字,(一定能够都是应用在对象类型变量上) 9 @property (nonatomic,strong)Car * car;//强直阵类型的对象,会影响对象回收 10 @property (nonatomic,weak)Car * car2;// 弱指针类型的对象,不会影响对象的回收 11 //@property (nonatomic,assign)Car * car3;//造成迷惑,会导致许多程序员搞不清这个变量到底是stong类型的还是weak类型 12 13 @property (nonatomic,strong)NSString * name; 14 @property (nonatomic,strong)NSString * idCard; 15 16 //ARC机制下 基本数据类型的@property参数使用,与手动内存管理完全一致 17 @property (nonatomic,assign)int age; 18 19 @end
Person.h
Car.m
1 #import "Car.h" 2 3 @implementation Car 4 - (void)dealloc 5 { 6 NSLog(@"Car 被销毁了"); 7 } 8 @end
Car.m
Car.h
1 #import <Foundation/Foundation.h> 2 3 @interface Car : NSObject 4 5 @end
Car.h
main.m
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 8 9 } 10 return 0; 11 }
main.m
05-手动内存管理转换成ARC机制
Person.m
1 #import "Person.h" 2 3 @implementation Person 4 - (void)dealloc 5 { 6 NSLog(@"PErson 被销毁了"); 7 } 8 @end
Person
Person.h
1 #import <Foundation/Foundation.h> 2 3 @interface Person : NSObject 4 5 @property (nonatomic,strong)NSString * name; 6 @property (nonatomic,strong)NSString * idCard; 7 8 @end
Person.h
main.m
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 8 // insert code here... 9 NSLog(@"Hello, World!"); 10 11 } 12 return 0; 13 }
main.m
06-ARC与非ARC共存
Person.m
1 #import "Person.h" 2 3 @implementation Person 4 - (void)sendMessageByIphone:(Iphone *)phone 5 { 6 [phone sendMessageWithNumber:@"123124321"]; 7 } 8 9 - (void)dealloc 10 { 11 // [super dealloc]; 12 } 13 @end
Person
Person.h
1 #import <Foundation/Foundation.h> 2 #import "Iphone.h" 3 4 @interface Person : NSObject 5 6 - (void)sendMessageByIphone:(Iphone *)phone; 7 8 @end
Person.h
Iphone.m
1 #import "Iphone.h" 2 3 @implementation Iphone 4 5 - (void)dealloc 6 { 7 [super dealloc]; 8 [_name release]; 9 NSLog(@"Iphone 被销毁了"); 10 } 11 12 13 - (void)sendMessageWithNumber:(NSString *)number 14 { 15 NSLog(@"发消息给 %@",number); 16 } 17 @end
Iphone.m
Iphone.h
1 #import <Foundation/Foundation.h> 2 3 @interface Iphone : NSObject 4 5 @property (nonatomic,retain)NSString * name; 6 7 - (void)sendMessageWithNumber:(NSString *)number; 8 9 @end
Iphone.h
main.m
1 #import <Foundation/Foundation.h> 2 #import "Person.h" 3 4 int main(int argc, const char * argv[]) 5 { 6 7 @autoreleasepool { 8 9 10 11 } 12 return 0; 13 }
main.m
07-模拟第三方程序
Iphone.m
1 #import "Iphone.h" 2 3 @implementation Iphone 4 5 - (void)dealloc 6 { 7 [super dealloc]; 8 [_name release]; 9 NSLog(@"Iphone 被销毁了"); 10 } 11 12 13 - (void)sendMessageWithNumber:(NSString *)number 14 { 15 NSLog(@"发消息给 %@",number); 16 } 17 @end
Iphone.m
Iphone.h
1 #import <Foundation/Foundation.h> 2 3 @interface Iphone : NSObject 4 5 @property (nonatomic,retain)NSString * name; 6 7 - (void)sendMessageWithNumber:(NSString *)number; 8 9 @end
Iphone.h
main.m
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 8 // insert code here... 9 NSLog(@"Hello, World!"); 10 11 } 12 return 0; 13 }
main.m
08-ARC类循环应用问题
Person.m
1 #import "Person.h" 2 3 @implementation Person 4 5 -(void)dealloc 6 { 7 NSLog(@"Person 被销毁了"); 8 } 9 10 @end
Person.m
Person.h
1 #import <Foundation/Foundation.h> 2 #import "Car.h" 3 4 @interface Person : NSObject 5 6 @property (nonatomic,strong)Car * car; 7 8 @end
person.h
Car.m
1 #import "Car.h" 2 #import "Person.h" 3 4 @implementation Car 5 6 - (void)dealloc 7 { 8 NSLog(@"Car 被销毁了"); 9 } 10 11 @end
Car.m
Car.h
1 import <Foundation/Foundation.h> 2 //#import "Person.h" 3 @class Person; 4 5 @interface Car : NSObject 6 7 //@property (nonatomic,strong)Person * p; 8 9 //当出现类循环应用的时候,只需要把一方面的Strong引用改为weak,并且在.h文件中使用@class 类名的方式,通知.h文件类的存在 10 @property (nonatomic,weak)Person * p; 11 12 @end
Car.h
main.m
1 #import <Foundation/Foundation.h> 2 #import "Person.h"; 3 4 int main(int argc, const char * argv[]) 5 { 6 7 @autoreleasepool { 8 9 Person * p = [[Person alloc] init]; 10 11 Car * car = [[Car alloc] init]; 12 13 [p setCar:car]; 14 15 [car setP:p]; 16 17 p = nil; 18 19 car = nil; 20 21 NSLog(@"agfadsgf"); 22 } 23 return 0; 24 }
main.m
时间: 2024-10-06 12:46:18