copy和mutableCopy方法

作者:追风剑情 发布于:2019-3-7 12:49 分类:Objective-C

示例一:数组元素是不可变字符串

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:
                @"one", @"two", @"three", @"four", nil];
        
        NSMutableArray *dataArray2;
        
        // 简单赋值
        dataArray2 = dataArray;
        [dataArray2 removeObjectAtIndex: 0];
        NSLog(@"dataArray:");
        for (NSString *elem in dataArray)
            NSLog(@" %@", elem);
        
        NSLog(@"dataArray2:");
        for (NSString *elem in dataArray2)
            NSLog(@" %@", elem);
        
        // 复制一份,然后删除副本的第一个元素
        // 产生一个对象的可变字符串并不要求被复制的对象本身是可变的。
        // 这种情况同样适用于不可变副本:可以创建可变对象的不可变副本。
        dataArray2 = [dataArray mutableCopy];
        [dataArray2 removeObjectAtIndex: 0];
        
        NSLog(@"dataArray: ");
        for (NSString *elem in dataArray)
        NSLog(@" %@", elem);
        
        NSLog(@"dataArray2:");
        for (NSString *elem in dataArray2)
            NSLog(@" %@", elem);
    }
    return 0;
}

运行测试
111.png

示例二:数组元素是可变字符串

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:
                [NSMutableString stringWithString: @"one"],
                [NSMutableString stringWithString: @"two"],
                [NSMutableString stringWithString: @"three"],
                nil];
        
        NSMutableArray *dataArray2;
        NSMutableString *mStr;
        
        NSLog(@"dataArray:");
        for (NSString *elem in dataArray)
            NSLog(@" %@", elem);
        
        // 复制一份,然后删除副本的第一个元素
        // mutableCopy仅复制了数组中可变字符串对象的引用
        dataArray2 = [dataArray mutableCopy];
        
        mStr = dataArray[0];
        [mStr appendString: @"ONE"];
        
        NSLog(@"dataArray: ");
        for (NSString *elem in dataArray)
        NSLog(@" %@", elem);
        
        NSLog(@"dataArray2:");
        for (NSString *elem in dataArray2)
            NSLog(@" %@", elem);
    }
    return 0;
}

运行测试
111.png

示例三:重写示例二

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:
                [NSMutableString stringWithString: @"one"],
                [NSMutableString stringWithString: @"two"],
                [NSMutableString stringWithString: @"three"],
                nil];
        
        NSMutableArray *dataArray2;
        NSMutableString *mStr;
        
        NSLog(@"dataArray:");
        for (NSString *elem in dataArray)
            NSLog(@" %@", elem);
        
        // 复制一份,然后删除副本的第一个元素
        // mutableCopy仅复制了数组中可变字符串对象的引用
        dataArray2 = [dataArray mutableCopy];
        
        //mStr = dataArray[0];
        //[mStr appendString: @"ONE"];
        
        // 对想改变的元素创建一个副本
        mStr = [NSMutableString stringWithString: dataArray2[0]];
        [mStr appendString: @"ONE"];
        [dataArray2 replaceObjectAtIndex: 0 withObject: mStr];
        
        NSLog(@"dataArray: ");
        for (NSString *elem in dataArray)
        NSLog(@" %@", elem);
        
        NSLog(@"dataArray2:");
        for (NSString *elem in dataArray2)
            NSLog(@" %@", elem);
    }
    return 0;
}

运行测试
111.png

标签: Objective-C

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号