0

I have an object with a 2d C array (can't figure out how to do the same with NSArray) and I also need this object to provide deep copies of itself. I'm trying to implement the NSCopying protocol except when trying to make a copy of the c array i can't figure out how to reference self's array and the copy's array. Since it's not a property and obj c doesn't support c array properties as far as i know, i don't know how to set the new copy's array.

I've tried typedefing my array as a struct but I'm also using ARC so that isn't a valid solution

Hopefully I'm not missing something basic. Thanks.

3
  • 2
    Post what you have so people can help you with the code. Commented Mar 25, 2014 at 3:22
  • since you mentioned the 2D NSArray quest, "How to create a 2D NSArray or NSMutableArray in Objective C" Commented Mar 25, 2014 at 3:24
  • Hi WhozCraig. My outer array can contain many inner arrays so I'd prefer to use a more elegant solution than having to init each array in a loop. But if i can't I'll probably settle on that. Commented Mar 25, 2014 at 3:35

1 Answer 1

2

You can use the -> notation to access the instance variables of the copied object. When making a deep copy, each object in the array must be copied.

// define a custom class to store in the array
@interface OtherClass : NSObject <NSCopying>
@property (nonatomic, strong) NSString *string;
@end

@implementation OtherClass
- (id)copyWithZone:(NSZone *)zone
{
    OtherClass *temp = [OtherClass new];
    temp.string = [self.string stringByAppendingString:@" (copy)"];
    return( temp );
}

- (void)dealloc
{
    NSLog( @"OtherClass dealloc: %@", self.string );
}
@end

// define the class that contains a C array of custom objects
@interface SomeClass : NSObject <NSCopying>
{
    OtherClass *array[5][5];
}
@end

@implementation SomeClass
- (id)copyWithZone:(NSZone *)zone
{
    SomeClass *temp = [SomeClass new];

    for ( int i = 0; i < 5; i++ )
        for ( int j = 0; j < 5; j++ )
            temp->array[i][j] = [array[i][j] copy];

    return( temp );
}

- (void)storeObject:(OtherClass *)object atRow:(int)row Col:(int)col
{
    array[row][col] = object;
    object.string = [NSString stringWithFormat:@"row:%d col:%d", row, col];
}

- (void)dealloc
{
    NSLog( @"SomeClass dealloc" );
}
@end

// test code to create, copy, and destroy the objects
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    SomeClass *a = [SomeClass new];

    for ( int i = 0; i < 5; i++ )
        for ( int j = 0; j < 5; j++ )
            [a storeObject:[OtherClass new] atRow:i Col:j];

    SomeClass *b = [a copy];

    NSLog( @"Releasing A" );
    a = nil;

    NSLog( @"Releasing B" );
    b = nil;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Looks good, just want to ask are there any additional memory management issues I should be aware of?
I don't believe so. AFAIK, the instance variables are dealloc'd when the object itself is dealloc'd. Of course, if you malloc items to put in the array, then you must override the dealloc method to free those items.
@K.He I guess I should ask, "What you are storing in the array?"
I'm storing a custom subclass of NSObject. Also does memcpy allocate separate memory from the original array?
ok, I was thinking that you were storing primitives. I will update the sample code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.