iOS - 基础知识

NSObject p = [NSObject new];
p release -> p指向的对象被销毁,内存不可用了,但是p 还存着原对象的地址,
p就成了野指针。(ARC下oc对象没有release,但是底层类还是有类似CFRelease,free等)
p = nil -> *p 为空指针,不指向任何内存地址。

野指针.png

注:p(A)原本指向B地址,存储C内容,当p release时,销毁了C内容,但是p(A)的指向依旧存在,B地址重复使用是,C内容改变,再调用p,就会异常了。也就是野指针。将p(A)=nil;就是将p(A)不再指向B。也就避免了野指针。ARC下的weak属性,会在内容销毁时自动置为nil。

  • 避免野指针
    一、指针变量初始化,要么将指针设置为NULL,要么让它指向合法的内存。
    二、指针p被free或者delete之后,置为NULL。

ARC 下:

strong 引用计数+1
weak,copy 引用计数不+1
对象不被持有时(引用计数0),即被销毁

** 属性参数**
原子性:atomic,nonatomic
读写属性:readwrite,readonly
set方法:assign,copy,strong,weak


  • assign:基础数据类型,数据存于栈内,编译时即确定其内存,直接赋值。

以下:对象类,存于堆,需要指针访问,动态分配与释放。

  • 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;// 新增一个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 内存地址依然存在,猜测局部NSString变量self持有,并且为copy属性,所以上面使用 NSMutableString,修改时,地址不变。
    NSLog(@"w%@",self.stringWeak);
    
    tempString = nil;
    NSLog(@"w%@",self.stringWeak);

关键字

  • static 静态全局变量:
    简单说:只能在某处调用,但拥有全局变量的生命周期。
    定义在方法内部:只能方法内部使用。
    定义在类:与全局变量类似,只供类内部使用。
- (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 右边的内容不能修改
NSString *const name = @"";// name 值不能修改
NSString const *address = @"";// address指针不能修改

1

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是KVC,什么是KVO,他们之间关系.底层实现 键值编码 Key-Value-Coding(KVC) 键值编码...
    Civel_Xu阅读 5,110评论 3 10
  • 4月10号跟公司提了辞职。说好不会立马走,留一两个月的缓冲期给公司。这样一来,立马辞职投简历找工作的计划也就搁浅...
    tiGress阅读 4,206评论 1 4
  • 多线程、特别是NSOperation 和 GCD 的内部原理。运行时机制的原理和运用场景。SDWebImage的原...
    LZM轮回阅读 6,102评论 0 12
  • iOS面试小贴士 ———————————————回答好下面的足够了------------------------...
    不言不爱阅读 6,258评论 0 7
  • 上个星期,党支部组织活动,作为宣传委员的我负责督促还没完成活动内容的同事,编辑好微信内容,找到通讯录里的“羽”,发...
    蓝莓欢歌阅读 2,976评论 1 6