iOS學習—深拷貝和淺拷貝


深拷貝就是生成一個新的對象,內(nèi)容和原對象完全相同。淺拷貝即是指針的拷貝,生成一個新的指針指向原對象。

//
//  main.m
//  深拷貝與淺拷貝
//
//  Created by on 15/4/10.
//  Copyright (c) 2015年 apple. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#import "Student.h"
#import "GoodStudent.h"
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        /*
            拷貝的意義:
                在改變副本對象的時候,不會影響到原對象
          
            定義:
                深拷貝:就是拷貝出來一個新的對象,內(nèi)容和原對象一樣。
                淺拷貝:其實就是指針的拷貝,沒有一個新的對象生成,只是多了一個指向原對象的指針,將原對象的指針賦值給了新聲明的指針,即,是一個指針的拷貝。
          
         */
         
//        只有從不可變對象copy到不可變對象的時候才是淺拷貝,其他的都是深拷貝
        NSString *string = @"abcde";
        NSString *str = [string copy];
        NSLog(@"string: %@", string);
        NSLog(@"str: %@", str);
         
//        因為指向同一個對象,所以原對象的引用計數(shù)+1
        NSLog(@"string和str是否是同一個對象: %d", string == str);
         
//        由可變字符到不可變字符的copy是深拷貝
        NSMutableString *mutableString = [NSMutableString stringWithFormat:@"abc%@", @"dddddd"];
        NSString *str1 = [mutableString copy];
        NSLog(@"mutableString和str1是否是同一個對象: %d", mutableString == str1);
         
         
//        mutalbeCopy都是深拷貝
        NSString *str2 = @"abbbbbbb";
        NSMutableString *mutableString1 = [str2 mutableCopy];
        NSLog(@"str2和mutableString1是否是同一個對象: %d", str2 == mutableString1);
//        由此可見,即使是不可變到不可變的mutableCopy也是深拷貝,進一步證明了mutableCopy是深拷貝
        NSString *str3 = @"kkkkkkk";
        NSString *string1 = [str3 mutableCopy];
        NSLog(@"str3和string1是否是同一個對象: %d", str3 == string1);
         
         
        NSMutableString *myStr1 = [NSMutableString stringWithFormat:@"abc%d", 10];
        NSMutableString *myStr2 = [myStr1 copy];
         
//        這里的myStr2實際是一個immutable對象,是不可變的,所以改變myStr2對象會報錯
//        [myStr2 appendString:@"fdsaf"];
 
        NSLog(@"myStr1: %@", myStr1);
        NSLog(@"myStr2: %@", myStr2);
        NSLog(@"myStr1和myStr2是否是同一個對象: %d", myStr1 == myStr2);
         
         
//        如果有改變副本的內(nèi)容而保持原來的對象內(nèi)容不變的話,那么就用copy屬性
//        這里改變了mutableName但是stu的name屬性值沒變
        Student *stu = [[Student alloc] init];
        NSMutableString *mutableName = [NSMutableString stringWithFormat:@"aaaaaa%d", 10];
        stu.name = mutableName;
        [mutableName appendString:@" fdsafdsafsad"];
        NSLog(@"stu.name: %@", stu.name);
        NSLog(@"mutableName: %@", mutableName);
        NSLog(@"stu.name和mutableName是否是同一個對象: %d", stu.name == mutableName);
         
         
         
         
        Student *stu1 = [Student studentWithName:@"stu1"];
        Student *stu2 = [stu1 copy];
        NSLog(@"%@", stu1.name);
        NSLog(@"%@", stu2.name);
         
        stu2.name = @"stu2";
        NSLog(@"%@", stu1.name);
        NSLog(@"%@", stu2.name);
         
         
         
         
        GoodStudent *goodStudent1 = [GoodStudent goodStudentWithAge:10 name:@"Jack"];
        GoodStudent *goodStudent2 = [goodStudent1 copy];
        goodStudent2.name = @"Tom";
        goodStudent2.age = 20;
        NSLog(@"goodStudent1 -> name: %@, age: %ld", goodStudent1.name, goodStudent1.age);
        NSLog(@"goodStudent2 -> name: %@, age: %ld", goodStudent2.name, goodStudent2.age);
    }
    return 0;
}
//
//  Student.h
//  深拷貝與淺拷貝
//
//  Created by on 15/4/10.
//  Copyright (c) 2015年 apple. All rights reserved.
//
 
#import <Foundation/Foundation.h>
 
@interface Student : NSObject <NSCopying>
 
@property (nonatomic, copy) NSString *name;
 
+ (id)studentWithName:(NSString *)name;
 
@end
//
//  Student.m
//  深拷貝與淺拷貝
//
//  Created by on 15/4/10.
//  Copyright (c) 2015年 apple. All rights reserved.
//
 
#import "Student.h"
 
@implementation Student
 
+ (id)studentWithName:(NSString *)name {
//    這里右邊需要寫成self class這樣,因為如果子類調(diào)用父類的這個方法的時候,就會產(chǎn)生相應的子類,而不是產(chǎn)生父類
//    左邊不用動,這表現(xiàn)了面向?qū)ο蟮奶攸c,多態(tài)
    Student *stu = [[[self class] alloc] init];
    stu.name = name;
    return stu;
}
 
- (id)copyWithZone:(NSZone *)zone {
    Student *copy = [[[self class] allocWithZone:zone] init];
    copy.name = self.name;
    return copy;
}
 
@end
//
//  GoodStudent.h
//  深拷貝與淺拷貝
//
//  Created by on 15/4/10.
//  Copyright (c) 2015年 apple. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#import "Student.h"
 
@interface GoodStudent : Student
 
@property (nonatomic, assign) NSInteger age;
 
+ (id)goodStudentWithAge:(int)age name:(NSString *)name;
 
@end
//
//  GoodStudent.m
//  深拷貝與淺拷貝
//
//  Created by on 15/4/10.
//  Copyright (c) 2015年 apple. All rights reserved.
//
 
#import "GoodStudent.h"
 
@implementation GoodStudent
 
+ (id)goodStudentWithAge:(int)age name:(NSString *)name {
    GoodStudent *good = [GoodStudent studentWithName:name];
     
    good.age = age;
     
    return good;
}
 
- (id)copyWithZone:(NSZone *)zone {
    // 一定要調(diào)用父類的方法
    GoodStudent *copy = [super copyWithZone:zone];
     
    copy.age = self.age;
     
    return copy;
}
 
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 深拷貝和淺拷貝這個問題在面試中常常被問到,而在實際開發(fā)中,只要稍有不慎,就會在這里出現(xiàn)問題。尤其對于初學者來說,我...
    西門淋雨閱讀 1,924評論 0 1
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,608評論 30 472
  • 307、setValue:forKey和setObject:forKey的區(qū)別是什么? 答:1, setObjec...
    AlanGe閱讀 1,718評論 0 1
  • 首先 這可能是一篇連載說的是我的故事一個十四歲青春期女孩的故事。 可能會跟許許多多初中生女孩一樣,明天打打鬧鬧,今...
    三字一個水閱讀 221評論 0 0

友情鏈接更多精彩內(nèi)容