首页 > 其他 > 详细

OC之Copy语法

时间:2014-03-29 10:46:28      阅读:487      评论:0      收藏:0      [点我收藏+]

转载请注明:http://www.cnblogs.com/letougaozao/p/3631105.html

  • 概念
  • 内存管理
  • NSString的copy实例
  • 对象的copy实例

一、概念

目的:在改变原有对象的时候,不会改变新对象的值

  1. Copy:实现NSCopying协议,创建的是一个不可变副本
  2. MutableCopy:实现NSMutableCopying协议,创建的是一个可变副本

二、内存管理

  1. 深拷贝:产生新的对象,所以源对象计数器不变>>>对象拷贝
  2. 浅拷贝:不产生新对象,所以源对象计数器加一>>>指针拷贝

三、NSString的copy实例

bubuko.com,布布扣
//
//  main.m
//  Copy语法
//
//  Created by apple on 14-3-28.
//  Copyright (c) 2014年 apple. All rights reserved.
//

#import <Foundation/Foundation.h>

void test1()
{
    NSString *str = [NSString stringWithFormat:@"age is %i", 10];
    NSString *str1 = [str copy];
    
    NSLog(@"%i", str == str1);
    
    NSString *str2 = [str mutableCopy];
    
    NSLog(@"%i", str2 == str);
}

void test2()
{
    NSMutableString *str = [NSMutableString stringWithFormat:@"age is %i", 11];
    
    NSString *str1 = [str copy];
    NSMutableString *str2 = [str mutableCopy];
    
    [str appendFormat:@"1"];
    
    NSLog(@"%i", str == str2);
    NSLog(@"%i", str == str1);
    NSLog(@"%@", str);
    NSLog(@"%@", str1);
}

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

    @autoreleasepool {
        
        test2();
        
    }
    return 0;
}
bubuko.com,布布扣

四、对象拷贝的实例

对象的拷贝,主要注意点

  1.  必须实现NSCopying协议
  2. 需要重写- (id)copyWithZone:(NSZone *)zone方法
  3. 代码中 self class的引用

1??GoodStudent.h

bubuko.com,布布扣
//
//  GoodStudent.h
//  Student的Copy用法
//
//  Created by apple on 14-3-28.
//  Copyright (c) 2014年 apple. All rights reserved.
//

#import "Student.h"

@interface GoodStudent : Student

@property (nonatomic, assign) int age;

+(id)goodStudentWithName:(NSString *)name withAge:(int)age;

@end
bubuko.com,布布扣

 

2??GoodStudent.m

bubuko.com,布布扣
//
//  GoodStudent.m
//  Student的Copy用法
//
//  Created by apple on 14-3-28.
//  Copyright (c) 2014年 apple. All rights reserved.
//

#import "GoodStudent.h"

@implementation GoodStudent

+(id)goodStudentWithName:(NSString *)name withAge:(int)age
{
    GoodStudent *stu = [super studentWithName:name];
    stu.age = age;
    
    return stu;
}

-(id)copyWithZone:(NSZone *)zone
{
    GoodStudent *copy = [super copyWithZone:zone];
    copy.age = self.age;
    
    return copy;
}

-(NSString *)description
{
    return [NSString stringWithFormat:@"%@-%i", self.name, self.age];
}

@end
bubuko.com,布布扣

 

3??Student.h

bubuko.com,布布扣
//
//  Student.h
//  Student的Copy用法
//
//  Created by apple on 14-3-28.
//  Copyright (c) 2014年 apple. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Student : NSObject <NSCopying>

@property (nonatomic, copy) NSString *name;

+(id)studentWithName:(NSString*)name;

@end
bubuko.com,布布扣

 

4??Student.m

bubuko.com,布布扣
//
//  Student.m
//  Student的Copy用法
//
//  Created by apple on 14-3-28.
//  Copyright (c) 2014年 apple. All rights reserved.
//

#import "Student.h"

@implementation Student
+(id)studentWithName:(NSString *)name
{
    Student *stu = [[[[self class] alloc] init] autorelease];
    stu.name = name;
    
    return stu;
}

- (id)copyWithZone:(NSZone *)zone
{
    Student *copy = [[self class] allocWithZone:zone];
    
    copy.name = self.name;
    
    return copy;
}

-(NSString *)description
{
    return [NSString stringWithFormat:@"%@", self.name];
}

-(void)dealloc
{
    [_name release];
    [super dealloc];
}
@end
bubuko.com,布布扣

 

main.m

bubuko.com,布布扣
//
//  main.m
//  Student的Copy用法
//
//  Created by apple on 14-3-28.
//  Copyright (c) 2014年 apple. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GoodStudent.h"

void test1()
{
    Student *stu = [Student studentWithName:@"name1"];
    Student *stu1 = [stu copy];
    
    NSLog(@"%@", stu);
    NSLog(@"%@", stu1);
}

void test2()
{
    GoodStudent *stu1 = [GoodStudent goodStudentWithName:@"name1" withAge:10];
    
    GoodStudent *stu2 = [stu1 copy];
    
    NSLog(@"%@", stu1);
    NSLog(@"%@", stu2);
    
}

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

    @autoreleasepool {
        
        test2();
        
    }
    return 0;
}
bubuko.com,布布扣

OC之Copy语法,布布扣,bubuko.com

OC之Copy语法

原文:http://www.cnblogs.com/letougaozao/p/3631105.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!