Objective-C个人学习系列(7) 内存管理与深 浅复制

#import <Foundation/Foundation.h>

#import "Person.h"

int main(int argc, const char * argv[])

{

    @autoreleasepool {

        

        //内存管理规则:只要有alloc、retain、new、copy、mutablecopy的地方,都要有对应的release。

        //[Object new] => [[Object alloc] init]

        NSString * string = [[NSString alloc]initWithFormat: @"123"];

        

        NSLog(@"string retainCount=%ld",[string retainCount]);

        

        [string retain];

        

        NSLog(@"string retainCount=%ld",[string retainCount]);

        

        [string release];

        

        NSLog(@"string retainCount=%ld",[string retainCount]);

        

        NSString * string2 = [string copy];     

         //NSString: copy => [string retain] NSString的copy 只是将计数器+1

        

        NSLog(@"string2 retainCount=%ld",[string2 retainCount]);

        NSLog(@"string retainCount=%ld",[string retainCount]);

        

        [string2 release];

        

        [string release];

        

        NSLog(@"string retainCount=%ld",[string retainCount]);

        

       

        

        //***********************copy 和 mutableCopy****************************

        //copy : 不可变复制,需要实现协议<NSCopying>

        NSString * _str = [[NSString alloc] initWithFormat:@"AAA"];

        Person * person = [[Person alloc] initWithName:_str sex:18];

        //

        Person *person2 = [person copy];

        

        NSLog(@"person->_name=%@",person->_name);           //person->_name=AAA

        NSLog(@"person2->_name=%@",person2->_name);         //person2->_name=AAA

        

        [person setName:@"BBB"];

        

        NSLog(@"不可变复制:");

        NSLog(@"person->_name=%@",person->_name);           //person->_name=BBB

        NSLog(@"person2->_name=%@",person2->_name);         //person2->_name=AAA

        

        //mutalbeCopy:可变复制,需要实现<NSMutalbeCopying>

        Person *person3 = [person mutableCopy];

        [person setName:@"CCC"];

        

        NSLog(@"可变复制:");

        NSLog(@"person->_name=%@",person->_name);           //person->_name=CCC

        NSLog(@"person3->_name=%@",person3->_name);         //person2->_name=BBB

        

        

        [_str release];

        [person release];

        [person2 release];

        [person3 release];

        

        

        //浅复制:在复制操作时,对于被复制的对象的每一层复制都是指针复制。

        //深复制:在复制操作时,对于被复制的对象至少有一层复制是对象复制。

        

        //*************************可变&不可变字符串的不可变复制与可变复制*******************

        //1、不可变字符串

        NSString * str = [[NSString alloc] initWithFormat:@"abc"];

        

        //1)不可变字符串的不可变复制(地址不变,只是引用计数+1)

        NSLog(@"不可变字符串的不可变复制");

        NSString *str2 = [str copy];

        NSLog(@"NSString &str = %p",str);         //str = 0x100105b00

        NSLog(@"NSString &str2 = %p",str2);       //str2 = 0x100105b00

        

        //2)不可变字符串的可变复制(地址改变,逻辑上可认为深度复制)

        NSLog(@"不可变字符串的可变复制");

        NSMutableString *str3 = [str mutableCopy];

        

        NSLog(@"NSMutableString &str = %p",str);         //str = 0x100105b00

        NSLog(@"NSMutableString &str3 = %p",str3);       //str3 = 0x100108a50

        

        NSLog(@"str = %@",str);         //str = abc

        NSLog(@"str3 = %@",str3);       //str3 = abc

        

        [str3 appendString:@"123"];

        

        NSLog(@"str = %@",str);         //str = abc

        NSLog(@"str3 = %@",str3);       //str3 = abc123

        

        //2、可变字符串

        NSMutableString *mStr = [[NSMutableStringalloc]initWithFormat:@"abcde"];

        

        //1)可变字符串的不可变复制(地址改变,复制字符串对象,并且复制字符串内容,深度copy)

        NSLog(@"可变字符串的不可变复制");

        NSMutableString *mStr2 = [mStr copy];

        

        NSLog(@"NSMutableString &mStr = %p",mStr);         // &mStr = 0x100206f80

        NSLog(@"NSMutableString &mStr2 = %p",mStr2);       //&mStr2 = 0x100205fd0

        

        NSLog(@"mStr = %@",mStr);                   //mStr = abcde

        NSLog(@"mStr2 = %@",mStr2);                 //mStr2 = abcde

        

        [mStr appendString:@"qqq"];

        

        NSLog(@"mStr = %@",mStr);                   //mStr = abcdeqqq

        NSLog(@"mStr2 = %@",mStr2);                 //mStr2 = abcde

        

        //2)可变字符串的可变复制(地址改变,复制字符串对象,同时复制字符串内容,深度copy)

        NSLog(@"可变字符串的可变复制");

        NSMutableString *mStr3 = [mStr mutableCopy];

        

        NSLog(@"NSMutableString &mStr = %p",mStr);         // &mStr = 0x100206b40

        NSLog(@"NSMutableString &mStr3 = %p",mStr3);       // &mStr3 = 0x100206de0

        

        NSLog(@"mStr = %@",mStr);           //mStr = abcde

        NSLog(@"mStr3 = %@",mStr3);         //mStr3 = abcde

        

        [mStr appendString:@"888"];

        

        NSLog(@"mStr = %@",mStr);           //mStr = abcde888

        NSLog(@"mStr3 = %@",mStr3);         //mStr3 = abcde

        

        [str release];

        [str2 release];

        [str3 release];

        

        [mStr release];

        [mStr2 release];

        [mStr3 release];

        

         

        

        //**********************可变&不可变字符串数组的不可变复制与可变复制************************

        NSMutableString* s1 = [[NSMutableStringalloc]initWithFormat:@"A"];

        NSMutableString* s2 = [[NSMutableStringalloc]initWithFormat:@"B"];

        NSMutableString* s3 = [[NSMutableStringalloc]initWithFormat:@"C"];

        //1、不可变字符串数组

        NSArray *array = [[NSArray alloc]initWithObjects:s1,s2,s3, nil];

        

        //1)不可变字符数组的不可变复制(地址不变,只是引用计数+1,同NSString)

        NSLog(@"不可变字符串数组的不可变复制");

        NSArray *array2 = [array copy];

        NSLog(@"&array = %p",array);             //array = 0x100203da0

        NSLog(@"&array2 = %p",array2);           //array2 = 0x100203da0

        

        //2)可变复制(地址改变,复制字符串数组对象,但字符数组中字符串内容不改变,深度copy)

        NSLog(@"不可变字符串数组的可变复制");

        NSArray *array3 = [array mutableCopy];

        

        NSLog(@"&array = %p",array);             //array = 0x100203da0

        NSLog(@"&array3 = %p",array3);           //array3 = 0x100203b20

        

        NSLog(@"array[0]->%@",array[0]);        //mArray->A

        NSLog(@"array3[0]->%@",array3[0]);      //mArray3->A

        //改变数组中字符串的值

        [array[0] appendString:@"123"];

        NSLog(@"array[0]->%@",array[0]);        //array[0]->A123

        NSLog(@"array3[0]->%@",array3[0]);      //array3[0]->A123

         

        //2、可变字符串数组

        NSMutableArray *mArray = [[NSMutableArray alloc]initWithObjects:s1,s2,s3, nil];

        

        //1)可变字符串数组的不可变复制(地址改变,复制字符对象,数组中字符对象内容不变,深度copy)

        NSLog(@"可变字符串数组的不可变复制");

        NSMutableArray *mArray2 = [mArray copy];

        NSLog(@"&mArray = %p",mArray);           //mArray = 0x100100310

        NSLog(@"&mArray2 = %p",mArray2);         //mArray2 = 0x10010c200

        NSLog(@"mArray->%@",mArray[0]);         //mArray->A

        NSLog(@"mArray2->%@",mArray2[0]);       //mArray2->A

        

        //改变数组中字符串的值

        [mArray[0] appendString:@"333"];

        

        NSLog(@"mArray->%@",mArray[0]);         //mArray->A333

        NSLog(@"mArray2->%@",mArray2[0]);       //mArray2->A333

        

        //2)可变字符串数组的可变复制(地址改变,复制数组对象,但数组中字符对象内容不变,深度copy)

        NSLog(@"可变字符串数组的可变复制");

        NSMutableArray *mArray3 = [mArray copy];

        NSLog(@"&mArray = %p",mArray);           //mArray = 0x100208440

        NSLog(@"&mArray3 = %p",mArray3);         //mArray3 = 0x100208f30

        NSLog(@"mArray->%@",mArray[0]);         //mArray->A333

        NSLog(@"mArray3->%@",mArray3[0]);       //mArray3->A333

        

        //改变数组中字符串的值

        [mArray[0] appendString:@"444"];

        

        NSLog(@"mArray->%@",mArray[0]);         //mArray->A333444

        NSLog(@"mArray3->%@",mArray3[0]);       //mArray2->A333444

        

        [s1 release];

        [s2 release];

        [s3 release];

        [array release];

        [array2 release];

        [array3 release];

        [mArray release];

        [mArray2 release];

        [mArray3 release];

    }

    return 0;

}

#import "Person.h"

@implementation Person

-(id)initWithName:(NSString *)name sex:(unsigned char)sex{

    self = [super init];

    if (self != nil ) {

        if (name==NULL) {

            return NULL;

        }

        _name = [name retain];

        _sex = sex;

    }

    returnself;

}

-(void)setName:(NSString *)name{

    [name retain];

    [_name release];

    _name = name;

}

-(id)copyWithZone:(NSZone *)zone{

    Person *person = [[Person allocWithZone:zone] initWithName:_name sex:_sex];

    return person;

}

- (id)mutableCopyWithZone:(NSZone *)zone{

    Person *person = [[Person allocWithZone:zone] initWithName:_name sex:_sex];

    return person;

}

-(void)dealloc{

    [_name release];

    _name = nil;

    [super dealloc];

}

@end

#import <Foundation/Foundation.h>

@interface Person : NSObject<NSCopying,NSMutableCopying>{

@public

    NSString* _name;

    unsignedchar _sex;

}

-(id)initWithName:(NSString*)name sex:(unsigned char)sex;

-(void)setName:(NSString*)name;

- (id)copyWithZone:(NSZone *)zone;

- (id)mutableCopyWithZone:(NSZone *)zone;

@end

#import "Person.h"

@implementation Person

-(id)initWithName:(NSString *)name sex:(unsigned char)sex{

    self = [super init];

    if (self != nil ) {

        if (name==NULL) {

            return NULL;

        }

        _name = [name retain];

        _sex = sex;

    }

    returnself;

}

-(void)setName:(NSString *)name{

    [name retain];

    [_name release];

    _name = name;

}

-(id)copyWithZone:(NSZone *)zone{

    Person *person = [[Person allocWithZone:zone] initWithName:_name sex:_sex];

    return person;

}

- (id)mutableCopyWithZone:(NSZone *)zone{

    Person *person = [[Person allocWithZone:zone] initWithName:_name sex:_sex];

    return person;

}

-(void)dealloc{

    [_name release];

    _name = nil;

    [super dealloc];

}

@end

相关推荐