NSObject p = [NSObject new];
p release -> p指向的對象被銷毀,內(nèi)存不可用了,但是p 還存著原對象的地址,p就成了野指針。(ARC下oc對象沒有release,但是底層類還是有類似CFRelease,free等)
p = nil -> *p 為空指針,不指向任何內(nèi)存地址。

野指針.png
注:p(A)原本指向B地址,存儲C內(nèi)容,當(dāng)p release時(shí),銷毀了C內(nèi)容,但是p(A)的指向依舊存在,B地址重復(fù)使用是,C內(nèi)容改變,再調(diào)用p,就會異常了。也就是野指針。將p(A)=nil;就是將p(A)不再指向B。也就避免了野指針。ARC下的weak屬性,會在內(nèi)容銷毀時(shí)自動置為nil。
-
避免野指針
一、指針變量初始化,要么將指針設(shè)置為NULL,要么讓它指向合法的內(nèi)存。
二、指針p被free或者delete之后,置為NULL。
ARC 下:
strong 引用計(jì)數(shù)+1
weak,copy 引用計(jì)數(shù)不+1
對象不被持有時(shí)(引用計(jì)數(shù)0),即被銷毀
** 屬性參數(shù)**
原子性:atomic,nonatomic
讀寫屬性:readwrite,readonly
set方法:assign,copy,strong,weak
- assign:基礎(chǔ)數(shù)據(jù)類型,數(shù)據(jù)存于棧內(nèi),編譯時(shí)即確定其內(nèi)存,直接賦值。
以下:對象類,存于堆,需要指針訪問,動態(tài)分配與釋放。
- copy:

![Uploading strong_451854.png . . .]
NSMutableString *tempString = [NSMutableString stringWithString:@"123"];//
self.stringCopy = tempString;
NSLog(@"c%@",self.stringCopy);
[tempString insertString:@"00" atIndex:0];
NSLog(@"c%@",self.stringCopy);
tempString = nil;
NSLog(@"c%@",self.stringCopy);
- strong :

strong.png
NSMutableString *tempString = [NSMutableString stringWithString:@"123"];//
self.stringStrong = tempString;
NSLog(@"s%@",self.stringStrong);
[tempString insertString:@"00" atIndex:0];
NSLog(@"s%@",self.stringStrong);
tempString = nil;
NSLog(@"s%@",self.stringStrong);
weak:

weak.png
NSMutableString *tempString = [NSMutableString stringWithString:@"123"];//
self.stringWeak = tempString;
NSLog(@"w%@",self.stringWeak);
[tempString insertString:@"00" atIndex:0];
NSLog(@"w%@",self.stringWeak);
tempString = nil;
NSLog(@"w%@",self.stringWeak);
對比weak1:

weak1.png
NSMutableString *tempString = [NSMutableString stringWithString:@"123"];//
self.stringWeak = tempString;
self.stringStrong = tempString;// 新增一個(gè)strong的指針
NSLog(@"w%@",self.stringWeak);
[tempString insertString:@"00" atIndex:0];
NSLog(@"w%@",self.stringWeak);
tempString = nil;
NSLog(@"w%@",self.stringWeak);
對比weak2:

weak2.png
NSMutableString *tempString = [NSMutableString stringWithString:@"123"];//
self.stringWeak = tempString;
self.stringStrong = tempString;
NSLog(@"w%@",self.stringWeak);
[tempString insertString:@"00" atIndex:0];
NSLog(@"w%@",self.stringWeak);
tempString = nil;
self.stringStrong = nil;// string的指針也置空
NSLog(@"w%@",self.stringWeak);
注:猜測
NSString *tempString = @"000";
self.stringWeak = tempString;
NSLog(@"w%@",self.stringWeak);
tempString = @"999";// 雖然重新賦值了,但是原tempString 內(nèi)存地址依然存在,猜測局部NSString變量self持有,并且為copy屬性,所以上面使用 NSMutableString,修改時(shí),地址不變。
NSLog(@"w%@",self.stringWeak);
tempString = nil;
NSLog(@"w%@",self.stringWeak);
關(guān)鍵字
- static 靜態(tài)全局變量:
簡單說:只能在某處調(diào)用,但擁有全局變量的生命周期。
定義在方法內(nèi)部:只能方法內(nèi)部使用。
定義在類:與全局變量類似,只供類內(nèi)部使用。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
static NSInteger num = 0;
// NSInteger num = 0
num ++ ;
NSLog(@"%zi",num);
}
- extern 供外部使用,大概意思我定義過了,你拿去用吧,一般用于全局常量,如通知名稱
extern NSString *const CZTestNotification;// .h
NSString *const CZTestNotification = @"CZTestNotification";//.m
- const 常量:
const 右邊的內(nèi)容不能修改
NSString *const name = @"";// name 值不能修改
NSString const *address = @"";// address指針不能修改
1