IOS學習—深拷貝和淺拷貝

來源:互聯網
上載者:User

標籤:

////  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 {        /*            拷貝的意義:                在改變副本對象的時候,不會影響到原對象                     定義:                深拷貝:就是拷貝出來一個新的對象,內容和原對象一樣。                淺拷貝:其實就是指標的拷貝,沒有一個新的對象產生,只是多了一個指向原對象的指標,將原對象的指標賦值給了新聲明的指標,即,是一個指標的拷貝。                  */        //        只有從不可變對象copy到不可變對象的時候才是淺拷貝,其他的都是深拷貝        NSString *string = @"abcde";        NSString *str = [string copy];        NSLog(@"string: %@", string);        NSLog(@"str: %@", str);        //        因為指向同一個對象,所以原對象的引用計數+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);                //        如果有改變副本的內容而保持原來的對象內容不變的話,那麼就用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這樣,因為如果子類調用父類的這個方法的時候,就會產生相應的子類,而不是產生父類//    左邊不用動,這表現了物件導向的特點,多態    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 {    // 一定要調用父類的方法    GoodStudent *copy = [super copyWithZone:zone];        copy.age = self.age;        return copy;}@end

IOS學習—深拷貝和淺拷貝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.