Objective-C 学习笔记6 复制

本文详细介绍了Objective-C中的深浅复制概念,并通过实例演示了如何实现非容器对象及容器对象的复制。此外,还提供了自定义类实现<NSCopying>协议的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、Objective-C 分深浅复制,可变和不可变复制

//
//  main.m
//  sample004
//
//  Created by echoliu on 13-1-24.
//  Copyright (c) 2013年 echoliu. All rights reserved.
//

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        // insert code here...
        // 复制 非容器对象 对象本身不可变
        NSString *str=@"Origin string ";//定义一个常用字符串
        NSString *strcopy=[str copy];
        NSLog(@" str is %@",str);
        NSLog(@" str copy is %@",strcopy);
        NSMutableString *strmcopy=[str mutableCopy];
        NSLog(@" str copy is %@",strmcopy);
        [strmcopy appendFormat:@" this is copy mutable"];
        NSLog(@" str copy is %@",strmcopy);

        //复制 非容器对象 对象本身可变
        NSMutableString * mstr=[NSMutableString stringWithString:@"origin"];
        NSString * mstrcopy=[mstr copy];
        NSMutableString *mstrmcopy=[mstr copy];//这里很奇怪,居然是不可变副本
        NSLog(@" str copy is %@",mstrcopy);
        NSLog(@" str copy is %@",mstrmcopy);
        //[mstrmcopy appendFormat:@"abc"];
        NSMutableString *mstrmcopy2=[mstr mutableCopy];
        [mstrmcopy2 appendFormat:@" abc"];
        NSLog(@" str copy is %@",mstrmcopy2);
        
        NSArray * array1=[NSArray arrayWithObjects:@"s",@"f",@"f", nil];
        NSArray * array1copy=[array1 copy];
        for(NSString * ms in array1){
            NSLog(@"%@",ms);
        }
        
        for(NSString * ms in array1copy){
            NSLog(@"%@",ms);
        }
        
        //mutable
        NSMutableArray *marraycopy=[array1 mutableCopy];
        [marraycopy  addObject:@" addobj"];
        for(NSString * ms in marraycopy){
            NSLog(@"%@",ms);
        }
        [marraycopy removeObjectAtIndex:0];//删除第一个元素
        for(NSString * ms in marraycopy){
            NSLog(@"%@",ms);
        }
        [marraycopy removeLastObject];//删除最后一个元素
        for(NSString * ms in marraycopy){
            NSLog(@"%@",ms);
        }
        [marraycopy removeAllObjects];
        [marraycopy removeLastObject];//删除最后一个元素
        for(NSString * ms in marraycopy){
            NSLog(@"%@",ms);
        }

        //定义一个内容可变的容器
        NSArray *marray1=[NSArray arrayWithObjects:[NSMutableString stringWithString: @"a"],@"b",@"c", nil];
        for(NSString * ms in marray1){
            NSLog(@"%@",ms);
        }
        //不可变复制
        NSArray *marray1copy=[marraycopy copy];
        //可变复制
        NSMutableArray *marraymcopy1=[marray1 mutableCopy];
        for(NSString * ms in marraymcopy1){
            NSLog(@"%@",ms);
        }
        //获取一个字符串
        NSMutableString *tstr=[marray1 objectAtIndex:0];
        NSLog(@"string copy is %@",tstr);
        tstr=@"other value";
        NSLog(@"string copy is %@",tstr);
        [tstr appendString:@"ok last add"];

        
    }
    return 0;
}

 类的复制 <NSCopying>协议实现,实现类的复制需要实现对该接口的实现,且需要在实现文件m中增加重写方法

如下:

头文件

//
//  samplecopy.h
//  sample004
//
//  Created by echoliu on 13-1-25.
//  Copyright (c) 2013年 echoliu. All rights reserved.
//

#import <Foundation/Foundation.h>
//NSCopying 协议
@interface samplecopy : NSObject<NSCopying>
-(void)print;
@end

实现文件

//
//  samplecopy.m
//  sample004
//
//  Created by echoliu on 13-1-25.
//  Copyright (c) 2013年 echoliu. All rights reserved.
//

#import "samplecopy.h"

@implementation samplecopy
-(id)copyWithZone:(NSZone *)zone{
    return  self;
}
-(void)print{
    NSLog(@"this is copy class");
}
@end

 

转载于:https://www.cnblogs.com/iosman/archive/2013/01/24/2875736.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值