年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 4592|回复: 1

第二十五讲:Objective-C内存管理之浅拷贝

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2014-1-22 20:19:28 | 显示全部楼层 |阅读模式
    上一讲简述了拷贝的基本原理,这一讲以一个类进行说明浅拷贝的效果
    1、创建一个类Car、使用<NSCopying>,<NSMutableCopying>协议对对象进行拷贝,自创的类必须实现拷贝中的 - (id)copyWithZone:(NSZone *)zone 方法或- (id)mutableCopyWithZone:(NSZone *)zone 方法;
    2、修改拷贝对象中的值,这里区分copy与retain在对象操作中的不同点:car1 = car指向的是同一对象,car1 = [car copy]创建了另外一个对象,修改对象中的值不影响原来对象的值,但对象中的使用的其他对象不拷贝;
    3、深拷贝则对对象引用的其他对象都进行拷贝(后续通过深拷贝说明);
    SACar.h
    1. //
    2. //  SACar.h
    3. //  Copy
    4. //
    5. //  Created by yusian on 14-1-22.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. @interface SACar : NSObject <NSCopying, NSMutableCopying>
    10. @property int year;
    11. @property (copy) NSString *name;
    12. @end
    复制代码
    SACar.m
    1. //
    2. //  SACar.m
    3. //  Copy
    4. //
    5. //  Created by yusian on 14-1-22.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "SACar.h"
    9. @implementation SACar
    10. - (id) copyWithZone:(NSZone *)zone {
    11.     SACar * c = [[[[self class] allocWithZone:zone] init] autorelease];
    12.     c.name = self.name;
    13.     c.year = self.year;
    14.     return c;
    15. }
    16. - (id) mutableCopyWithZone:(NSZone *)zone {
    17.     SACar * c = [[[[self class] allocWithZone:zone] init] autorelease];
    18.     c.name = self.name;
    19.     c.year = self.year;
    20.     return c;
    21. }
    22. - (NSString *) description {
    23.     return [NSString stringWithFormat:@"%@ age is %d", self.name, self.year];
    24. }
    25. - (void) dealloc {
    26.     NSLog(@"%@ is dealloc", self.name);
    27.     [super dealloc];
    28. }
    29. @end
    复制代码
    main.m
    1. //
    2. //  main.m
    3. //  Copy
    4. //
    5. //  Created by yusian on 14-1-22.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "SACar.h"
    10. int main(int argc, const char * argv[])
    11. {
    12.     @autoreleasepool {
    13.         
    14.         SACar * car = [[[SACar alloc] init] autorelease];
    15.         car.name = @"Cruze";
    16.         car.year = 2013;
    17.         NSLog(@"%@", car);
    18.         SACar * car1 = [car copy];
    19.         car1.name = @"Sonata";
    20.         car1.year = 2014;
    21.         NSLog(@"%@", car1);
    22.         
    23.         NSLog(@"%@ retainCount is %ld", car.name, car.retainCount);
    24.         NSLog(@"%@ retainCount is %ld", car1.name, car1.retainCount);
    25.         
    26.         [car retain];
    27.         NSLog(@"%@ retainCount is %ld", car.name, car.retainCount);
    28.         NSLog(@"%@ retainCount is %ld", car1.name, car1.retainCount);
    29.         [car release];
    30.     }
    31.     return 0;
    32. }
    复制代码
    输出结果:
    1. 2014-01-22 20:14:08.733 Copy[1788:303] Cruze age is 2013
    2. 2014-01-22 20:14:08.735 Copy[1788:303] Sonata age is 2014
    3. 2014-01-22 20:14:08.735 Copy[1788:303] Cruze retainCount is 1
    4. 2014-01-22 20:14:08.735 Copy[1788:303] Sonata retainCount is 1
    5. 2014-01-22 20:14:08.736 Copy[1788:303] Cruze retainCount is 2
    6. 2014-01-22 20:14:08.736 Copy[1788:303] Sonata retainCount is 1
    7. 2014-01-22 20:14:08.736 Copy[1788:303] Sonata is dealloc
    8. 2014-01-22 20:14:08.737 Copy[1788:303] Cruze is dealloc
    9. Program ended with exit code: 0
    复制代码
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

     楼主| 发表于 2014-1-22 20:23:47 | 显示全部楼层
    如果将使用赋值,则效果如下:
    main.m
    1. //
    2. //  main.m
    3. //  Copy
    4. //
    5. //  Created by yusian on 14-1-22.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "SACar.h"
    10. int main(int argc, const char * argv[])
    11. {
    12.     @autoreleasepool {
    13.         
    14.         SACar * car = [[[SACar alloc] init] autorelease];
    15.         car.name = @"Cruze";
    16.         car.year = 2013;
    17.         NSLog(@"%@", car);
    18.         SACar * car1 =  car;
    19.         car1.name = @"Sonata";
    20.         car1.year = 2014;
    21.         NSLog(@"%@", car1);
    22.         NSLog(@"%@", car);
    23.         
    24.         NSLog(@"%@ retainCount is %ld", car.name, car.retainCount);
    25.         NSLog(@"%@ retainCount is %ld", car1.name, car1.retainCount);
    26.         
    27.         [car retain];
    28.         NSLog(@"%@ retainCount is %ld", car.name, car.retainCount);
    29.         NSLog(@"%@ retainCount is %ld", car1.name, car1.retainCount);
    30.         [car release];
    31.     }
    32.     return 0;
    33. }
    复制代码
    输出结果为:
    1. 2014-01-22 20:23:00.775 Copy[1885:303] Cruze age is 2013
    2. 2014-01-22 20:23:00.776 Copy[1885:303] Sonata age is 2014
    3. 2014-01-22 20:23:00.777 Copy[1885:303] Sonata age is 2014
    4. 2014-01-22 20:23:00.777 Copy[1885:303] Sonata retainCount is 1
    5. 2014-01-22 20:23:00.777 Copy[1885:303] Sonata retainCount is 1
    6. 2014-01-22 20:23:00.777 Copy[1885:303] Sonata retainCount is 2
    7. 2014-01-22 20:23:00.778 Copy[1885:303] Sonata retainCount is 2
    8. 2014-01-22 20:23:00.778 Copy[1885:303] Sonata is dealloc
    9. Program ended with exit code: 0
    复制代码
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-11-23 20:34 , Processed in 0.045211 second(s), 19 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表