iOS的集合深拷贝

集合的递归深拷贝

关于拷贝的一些概念在iOS集合的深复制与浅复制里 讲得很清楚啦.
我在这里想说的是递归深拷贝:

copy只做第一级的深拷贝。 如果array里面存的是指针,它就会把指针值做深拷贝,等于是后面的数组跟前面的数组,存的指针值是一样的,但是指针指向的内容不做深拷贝,所以改了指针指向的内容,会同时影响两个数组。

如果需要每一层都深拷贝的话:

NSMutableArray* a = [NSMutableArray arrayWithObjects:@"123",@"345", nil];

NSMutableArray* b = [NSMutableArray arrayWithObjects:@"xyz",@"abc", nil];

NSArray *c = [NSArray arrayWithObjects:a, b, nil];

NSArray *d = [c copyDeeply]; // 深拷贝

    for (NSArray* arr in d) {

        for (NSString* s in arr) {

            NSLog(@"element: %@", s);

        }

    }

    

    a[0] = @"haha";

    

    for (NSArray* arr in d) {

        for (NSString* s in arr) {

            NSLog(@"element: %@", s);

        }

    }

结果:

2017-04-05 11:26:23.688 Test[1341:101216] element: 123
2017-04-05 11:26:23.688 Test[1341:101216] element: 345
2017-04-05 11:26:23.688 Test[1341:101216] element: xyz
2017-04-05 11:26:23.688 Test[1341:101216] element: abc
2017-04-05 11:26:23.689 Test[1341:101216] element: 123
2017-04-05 11:26:23.689 Test[1341:101216] element: 345
2017-04-05 11:26:23.689 Test[1341:101216] element: xyz
2017-04-05 11:26:23.689 Test[1341:101216] element: abc

******************************************

NSArray 递归深拷贝

- (id)copyDeeply {

    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[self count]];
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        id item = obj;
        if ([item respondsToSelector:@selector(copyDeeply)]) {
            item = [item copyDeeply];
        } else {
            item = [item copy];
        }
        if (item) {
            [array addObject:item];
        }
    }];
    return [NSArray arrayWithArray:array];
}

- (id)mutableCopyDeeply {

    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[self count]];
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        id item = obj;
        if ([item respondsToSelector:@selector(mutableCopyDeeply)]) {
            item = [item mutableCopyDeeply];
        } else if ([item respondsToSelector:@selector(mutableCopyWithZone:)]) {
            item = [item mutableCopy];
        } else {
            item = [item copy];
        }
        if (item) {
            [array addObject:item];
        }
    }];
    return array;
}

NSDictionary 递归深拷贝


- (id)copyDeeply {
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:[self count]];
    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        id item = obj;
        if ([item respondsToSelector:@selector(copyDeeply)]) {
            item = [item copyDeeply];
        } else {
            item = [item copy];
        }
        if ( item && key ) {
            [dic setObject:item forKey:key];
        }
    }];
    return [NSDictionary dictionaryWithDictionary:dic];
}

- (id)mutableCopyDeeply {
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:[self count]];
    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        id item = obj;
        if ([item respondsToSelector:@selector(mutableCopyDeeply)]) {
            item = [item mutableCopyDeeply];
        } else if ([item respondsToSelector:@selector(mutableCopyWithZone:)]) {
            item = [item mutableCopy];
        } else {
            item = [item copy];
        }
        if ( item && key ) {
            [dic setObject:item forKey:key];
        }
    }];
    return dic;
}

你可能感兴趣的:(iOS的集合深拷贝)