天天看點

面試什麼時候用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 
 */