IOS階段學習第21天筆記(ARC記憶體管理-Copy-代理),ios-copy-
IOS學習(OC語言)知識點整理
一、OC 中的ARC記憶體管理
1)ARC中釋放對象的記憶體原則:看這個對象有沒有強引用指向它
2)strong:強引用,預設情況下的引用都是強引用
3) weak:弱引用__weak
4)ARC環境下:與記憶體相關的代碼都不能使用了,如果要在ARC環境下使用MRC記憶體管理代碼 如:
[super delloc] 選中項目找到 Build Phases 菜單下的 Compile Sources 項 選中要轉換的.m檔案,
雙擊寫入此行代碼:-fno-objc-arc 即可
5)將MRC代碼轉換成ARC代碼 操作:可以在Xcode 左上方Edit 菜單下的Convert中選擇To Objective-C ARC…
項設定一下即可
6)如果兩個對象的兩個指標互相引用,會出現對象的記憶體無法釋放的問題,解決辦法:一端用strong,
一端用weak, 如果是peroperty中的基本類型用assign
7)ARC 操作 執行個體代碼:
1 //main.m中的代碼 2 Person *p1=[[Person alloc]init]; 3 //上一個對象沒有強引用指向,記憶體會被釋放掉 4 p1=[[Person alloc]init]; 5 NSLog(@"********"); 6 //錯誤的寫法,表明有一個弱引用指向對象,這條語句執行完畢後對象就被釋放了 7 __weak Person *p2=[[Person alloc]init]; 8 NSLog(@"+++======="); 9 10 //Person.h中的代碼11 #import <Foundation/Foundation.h>12 @class Card;13 @interface Person : NSObject14 @property(nonatomic,strong)Card *card;15 @property(nonatomic,strong)NSString *name;16 @property(nonatomic,assign)int age;17 @end18 19 //Card.h中的代碼20 #import <Foundation/Foundation.h>21 #import "Person.h"22 @interface Card : NSObject23 @property(nonatomic,weak)Person *person;24 @end
二、copy與mutableCopy介紹
1)給字串發送copy訊息,得到的是一個不可變字串(不管是給可變還是不可變字串發送的訊息)
2)給字串發送mutableCopy訊息,得到的是一個可變字串(不管是給可變還是不可變字串發送的訊息)
3)字串對象的copy操作 執行個體:
1 NSString *str1=@"Hello"; 2 NSString *str2=str1; 3 //對不可變字串發送copy訊息會得到一個新的不可變字串 4 NSString *str3=[str1 copy]; 5 // [str3 appendString:@" shanghai"]; 6 //對不可變字串發送mutableCopy訊息後會得到一個新的可變字串 7 NSMutableString *str4=[str1 mutableCopy]; 8 [str4 appendString:@" beijing"]; 9 NSLog(@"str4:%@",str4);10 NSMutableString *mStr1=[NSMutableString stringWithString:str1];11 //對可變字串發送copy訊息會得到一個新的不可變字串12 NSString *mStr2=[mStr1 copy];13 //[mStr2 appendString:@"aaa"];14 15 //對可變字串發送mutableCopy訊息會得到一個新的可變字串對象16 NSMutableString *mStr3=[mStr1 mutableCopy];17 [mStr3 appendString:@"abc"];18 NSLog(@"mStr3=%@",mStr3);
4)實現對自訂對象的copy 執行個體代碼:
1、定義一個QFCar 類 .h檔案中的代碼如下:
1 #import <Foundation/Foundation.h> 2 @interface QFCar : NSObject<NSCopying>3 @property(nonatomic,copy)NSString *name;4 @property(nonatomic,assign)int year;5 -(void)print;6 @end
2、.m檔案中的代碼如下:
1 #import "QFCar.h" 2 @implementation QFCar 3 //調用copy方法時會執行此協議方法 4 - (id)copyWithZone:(NSZone *)zone 5 { 6 //QFCar *car=[[QFCar allocWithZone:zone]init]; 7 //可以被子類繼承,copy出來的就是子類的對象 8 //[self class]擷取當前的類(對象) 9 QFCar *car=[[[self class] allocWithZone:zone]init];10 car.name=self.name;11 car.year=self.year;12 return car;13 }14 15 -(void)print16 {17 18 NSLog(@"name is %@, year is %d",_name,_year);19 }20 @end
3、main 檔案中的實現代碼:
1 QFCar *mCar=[[QFCar alloc]init];2 NSString *n1=@“寶馬X6”;3 NSMutableString *n=[[NSMutableString alloc]initWithString:n1];4 mCar.name=n;5 mCar.year=2015;6 [mCar print];7 [n appendString:@" kingkong"];8 [mCar print];
5)淺拷貝與深拷貝的介紹
1、 此兩種拷貝是針對數字或字典集合來說的。
2、淺拷貝只拷貝數組對象,兩個數組對象存放的是相同元素的地址,數組中的元素對象並沒有被拷貝
3、深拷貝 不僅拷貝數組對象,數組中存放的元素對象也將被拷貝一份新的
6)深拷貝與淺拷貝執行個體代碼
1 NSMutableArray *carList=[[NSMutableArray alloc]init]; 2 for(int i=0;i<5;i++){ 3 QFCar *car=[[QFCar alloc]init];//自訂的類對象 4 car.name=[NSString stringWithFormat:@“寶馬X%d”,i+1]; 5 car.year=2011+i; 6 [carList addObject:car]; 7 } 8 9 //使用copy(mutableCopy)實現的是數組的淺拷貝(只拷貝數組對象,2個數組對象存放的是相同元素的地址,數組中的元素對象並沒有被拷貝)10 //NSMutableArray *array1=[carList mutableCopy];11 12 //淺拷貝13 NSMutableArray *array1=[[NSMutableArray alloc]initWithArray:carList];14 15 //實現數組的深拷貝(不僅拷貝數組對象,數組中存放的元素對象也將被拷貝一份新的)16 NSMutableArray *array2=[[NSMutableArray alloc]initWithArray:carList copyItems:YES];17 [[array2 lastObject] setName:@"shanghai"];18 for(QFCar *car in array2){19 [car print];20 }21 NSLog(@"***********");22 for(QFCar *car in carList){23 [car print];24 }
三、OC 中的代理介紹
1)代理即自己聲明方法自己不實現讓別的類或對象去實現的一個過程,代理的目的在於降低代碼的耦合性
2)代理一般都需要有一個協議(protocol)代理方需要遵守協議去實現方法
3)代理執行個體代碼
1、建立兩個類對象 HeadMaster(校長類) Teacher(教師類) ;教師類代理校長類的examine、
meeting、travle 三個方法
2、HeadMaster.h 檔案中的代碼實現如下:
1 #import <Foundation/Foundation.h> 2 @protocol MasterDelegate<NSObject> 3 -(void)examine; 5 -(void)meeting; 7 -(void)travle; 9 @end11 @interface HeadMaster : NSObject13 @property(nonatomic,strong)id<MasterDelegate> delegate;15 -(void)masterExamine;17 -(void)masterMeeting;19 -(void)masterTravle;21 @end
3、HeadMaster.m 檔案中的代碼實現如下:
1 #import "HeadMaster.h" 2 @implementation HeadMaster 3 -(void)masterExamine 4 { 5 if([_delegate conformsToProtocol:@protocol(MasterDelegate)]){ 6 if([_delegate respondsToSelector:@selector(examine)]){ 7 [_delegate examine]; 8 } 9 }10 } 12 -(void)masterMeeting13 {14 if([_delegate conformsToProtocol:@protocol(MasterDelegate)]){15 if([_delegate respondsToSelector:@selector(meeting)]){16 [_delegate meeting];17 }18 }19 }20 21 -(void)masterTravle22 {23 if([_delegate conformsToProtocol:@protocol(MasterDelegate)]){24 if([_delegate respondsToSelector:@selector(travle)]){25 [_delegate travle];26 }27 }28 }29 @end
4、Teacher.h檔案中的代碼實現如下:
1 #import <Foundation/Foundation.h>2 #import "HeadMaster.h"3 @interface Teacher : NSObject<MasterDelegate>4 @end
5、Teacher.m 檔案中的代碼實現如下:
1 #import "Teacher.h" 2 @implementation Teacher 3 -(void)examine 4 { 5 NSLog(@"teacher examine"); 6 } 7 8 -(void)meeting 9 {10 NSLog(@"teacher meeting");11 }12 13 -(void)travle14 {15 NSLog(@"teacher travle");16 }17 @end
6、main.m 檔案中的(實現調用)代碼 如下:
1 #import <Foundation/Foundation.h> 2 #import "Teacher.h" 3 int main(int argc, const char * argv[]) { 4 @autoreleasepool { 5 HeadMaster *master=[[HeadMaster alloc]init]; 6 Teacher *xiaozhang=[[Teacher alloc]init]; 7 master.delegate=xiaozhang; 8 [master masterExamine]; 9 [master masterMeeting];10 [master masterTravle];11 }12 return 0;13 }