面试什么时候用copy,strong,@property本质

面试经常问什么时候用copy,strong
不可变的用copy修饰   可变的用strong

- (void)testNOChange
{
    NSString *str = @"123";
    NSLog(@"str = %p",str);
    str = @"456";
    NSLog(@"after str = %p",str);
    /*
     2021-07-22 14:01:48.151382+0800 ProtocolTest[7759:511625] str = 0x104c0c0f8
     2021-07-22 14:01:48.151511+0800 ProtocolTest[7759:511625] after str = 0x104c0c138
     不一样,是重新复制后,重新分配一块地址
     */
}
- (void)testChange
{
    NSMutableString *str = [NSMutableString stringWithString:@"123"];
    NSLog(@"str = %p",str);
    [str appendString:@"456"];
    NSLog(@"after str = %p",str);
    /*
     2021-07-22 12:13:51.315687+0800 ProtocolTest[7333:492834] str = 0x2809155c0
     2021-07-22 12:13:51.315782+0800 ProtocolTest[7333:492834] after str = 0x2809155c
     地址一致
     */
}
- (void)testCopyString
{
    NSString *tempStr = @"123";
    NSMutableString *mutaStr = [[NSMutableString alloc] initWithString:tempStr];
    self.strCopy = mutaStr;//子类初始化父类
    NSLog(@"tempStr = %p mutaStr = %p self.strStrong = %p",tempStr,mutaStr,self.strCopy);
    [mutaStr appendString:@"asd"];
    NSLog(@"tempStr = %p mutaStr = %p self.strStrong = %p",tempStr,mutaStr,self.strCopy);
    NSLog(@"tempStr值 = %@ mutaStr值 = %@ self.strStrong值 = %@",tempStr,mutaStr,self.strCopy);
    /*
     @property  (nonatomic, copy) NSString   *strCopy; 不变值用copy修饰
     2021-07-22 14:34:32.817359+0800 ProtocolTest[7904:518114] tempStr = 0x1025100f8 mutaStr = 0x28187a220 self.strStrong = 0xae3f8149a61d34f9
     2021-07-22 14:34:32.817459+0800 ProtocolTest[7904:518114] tempStr = 0x1025100f8 mutaStr = 0x28187a220 self.strStrong = 0xae3f8149a61d34f9
     2021-07-22 14:34:32.817545+0800 ProtocolTest[7904:518114] tempStr值 = 123 mutaStr值 = 123asd self.strStrong值 = 123
     地址一样 self.strStrong值 没变
     */
}
- (void)testStrongString
{
    NSString *tempStr = @"123";
    NSMutableString *mutaStr = [[NSMutableString alloc] initWithString:tempStr];
    self.strStrong = mutaStr; //子类初始化父类
    NSLog(@"tempStr = %p mutaStr = %p self.strStrong = %p",tempStr,mutaStr,self.strStrong);
    [mutaStr appendString:@"asd"];
    NSLog(@"tempStr = %p mutaStr = %p self.strStrong = %p",tempStr,mutaStr,self.strStrong);
    NSLog(@"tempStr值 = %@ mutaStr值 = %@ self.strStrong值 = %@",tempStr,mutaStr,self.strStrong);
    /*
     @property  (nonatomic, strong) NSString *strStrong;
     2021-07-22 14:31:13.330844+0800 ProtocolTest[7889:517312] tempStr = 0x1005b80f8 mutaStr = 0x28237e070 self.strStrong = 0x28237e070
     2021-07-22 14:31:13.330947+0800 ProtocolTest[7889:517312] tempStr = 0x1005b80f8 mutaStr = 0x28237e070 self.strStrong = 0x28237e070
     2021-07-22 14:31:13.331002+0800 ProtocolTest[7889:517312] tempStr值 = 123 mutaStr值 = 123asd self.strStrong值 = 123asd
     地址一样,但是strStrong值被改了
     */
}
- (void)testCopyMutableString
{
    NSString *str = @"123";
    self.mutStrCopy = [[NSMutableString alloc] initWithString:str];
    NSLog(@"str = %p  self.mutStrCopy = %p",str,self.mutStrCopy);
    [self.mutStrCopy appendString:@"qwe"];
    /*
     @property  (nonatomic, copy) NSMutableString *mutStrCopy;
     str = 0x1005240f8  self.mutStrCopy = 0x89642021483a33b9
     [NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0x89642021483a33b9
     崩溃  self.mutStrCopy通过copy变成了不可变字符串 ,没有appendString方法
      不能用copy修饰NSMutableString  要用strong
     */
}
- (void)testStrongMutableString
{
    NSString *str = @"123";
    self.mutStrStrong = [[NSMutableString alloc] initWithString:str];
    NSLog(@"str = %p  self.mutStrCopy = %p",str,self.mutStrStrong);
    [self.mutStrStrong appendString:@"qwe"];
    NSLog(@"str after = %p  self.mutStrCopy after = %p",str,self.mutStrStrong);
    NSLog(@"str after值 = %@  self.mutStrCopy after值 = %@",str,self.mutStrStrong);
    /*
     @property  (nonatomic, strong) NSMutableString *mutStrStrong;
     2021-07-22 14:47:57.488192+0800 ProtocolTest[7959:521300] str = 0x1000cc0f8  self.mutStrCopy = 0x282a0aaf0
     2021-07-22 14:47:57.488288+0800 ProtocolTest[7959:521300] str after = 0x1000cc0f8  self.mutStrCopy after = 0x282a0aaf0
     2021-07-22 14:47:57.488342+0800 ProtocolTest[7959:521300] str after值 = 123  self.mutStrCopy after值 = 123qwe
     正确使用  可变的用strong
     */
}

- (void)testCopy {
    NSString *str1 = @"asdf";
    NSString *str2 = [str1 copy];
    NSMutableString *str3 = [str1 mutableCopy];
    NSLog(@"str1 = %p str2 = %p str3 = %p",str1,str2,str3);
    
    NSArray *arr1 = @[@"sfkkf"];
    NSArray *arr2 = [arr1 copy];
    NSMutableArray *arr3 = [arr1 mutableCopy];
    NSLog(@"arr1 = %p, arr2 = %p arr3 = %p",arr1,arr2,arr3);
    /*
     2021-07-22 18:21:26.378151+0800 ProtocolTest[90574:34420671] str1 = 0x1002f4268 str2 = 0x1002f4268 str3 = 0x281029770
     2021-07-22 18:21:26.378186+0800 ProtocolTest[90574:34420671] arr1 = 0x281c48770, arr2 = 0x281c48770 arr3 = 0x281029800
     不可变的  copy 地址一样  浅拷贝  mutableCopy地址变了 深拷贝
     */
}
- (void)testMutabCopy {
    NSMutableString *str1 = [[NSMutableString alloc] initWithString:@"asd"];
    NSString *str2 = [str1 copy];
    NSMutableString *str3 = [str1 mutableCopy];
    NSLog(@"str1 = %p str2 = %p str3 = %p",str1,str2,str3);
    
    NSMutableArray *arr1 = [NSMutableArray arrayWithArray:@[@"111"]];
    NSMutableArray *arr2 = [arr1 copy];
    NSMutableArray *arr3 = [arr1 mutableCopy];
    NSLog(@"arr1 = %p, arr2 = %p arr3 = %p",arr1,arr2,arr3);
    /*
     2021-07-22 17:40:58.793091+0800 ProtocolTest[8694:557542] str1 = 0x2819d8c00 str2 = 0x99b0d6976a55ab56 str3 = 0x2819d8bd0
     2021-07-22 17:40:58.793225+0800 ProtocolTest[8694:557542] arr1 = 0x2819d8ba0, arr2 = 0x281584830 arr3 = 0x2819d83f0
     可变的 无论copy mutableCopy都是深拷贝
     */
}

@paoperty 修饰的属性  = 成员变量  +  setter方法 + getter方法
 

/*
   @property  name  编译器默认添加了  成员变量_name/_sex + set方法 + get方法
   @synthesize age = _age;  编译器声明,自动合成 setter/getter方法
   @dynamic sex    ,编译器不自动生成setter/getter方法  ,需要手动生成
 
   修饰符  nonatomic copy strong assign
   读写权限  readwrite readonly
   weak  修饰对象  释放时 为nil  不会崩溃
   assign  修饰基本数据 如果修饰对象的话,释放后 野指针
    assign修饰的基本数据是在栈里面 , 1M左右,系统控制内存,会出现栈溢出,但是空间是连续的,不会出现野指针
    weak的对象时存在堆里,是有碎片的,程序员控制,链表结构,出现野指针
 
    不可变的用copy    可变的用strong 
 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值