0

Currently I’m trying to get the hang of a block copy with my current project. The structure of the copy is an NSMutableArray that contains NSIntegers, NSStrings another NSMutableArray and two Objects… Those objects in turn hold NSStrings. The Array contains Objects which hold an NSInteger and Two Objects which contain strings…

I believe I am supposed to use the Block Copy method for coping objects… Code is below…

I am aware the code is not releasing properly… I tried to make the code smaller for your benefit.

Any insight you could shed would be awesome.

//Main controller Excerpt
//Insert Position Information into temporary node point... Node Points can have multiple Positions (or rather you can face multiple directions at the node. Each Node has 3-4 of these.
[newNode.positionArray insertObject:[newPosition copy] atIndex:currentPosition];
Insert the temporary node into the Node Array.
[nodeArray insertObject:[newNode copy] atIndex:count];

//Main Controller Excerpt

//
//  Node.h
//

#import <Foundation/Foundation.h>

@class Sequence;
@class Position;

@interface Node : NSObject  {
    NSInteger Id;
    NSInteger currentPosition;
    NSString *title;
    NSMutableArray *positionArray;
    Sequence *forwardSequence;
    Sequence *backSequence;
}

-(id) copyWithZone: (NSZone *) zone;

@property (nonatomic, assign) NSInteger Id;
@property (nonatomic, assign) NSInteger currentPosition;
@property (nonatomic, assign) NSString *title;
@property (nonatomic, retain) NSMutableArray *positionArray;
@property (nonatomic, retain) Sequence *forwardSequence;
@property (nonatomic, retain) Sequence *backSequence;

@end


//
//  Node.m
//

#import "Sequence.h"
#import "Position.h"
#import "Node.h"

@implementation Node

@synthesize Id;
@synthesize currentPosition;
@synthesize positionArray;
@synthesize title;
@synthesize forwardSequence;
@synthesize backSequence;

-(id) copyWithZone: (NSZone *) zone {
    Node *nodeCopy = [[Node allocWithZone: zone] init];

    nodeCopy.Id = Id;
    nodeCopy.currentPosition    = currentPosition;
    nodeCopy.positionArray      = [positionArray copy];
    nodeCopy.title              = title;
    nodeCopy.forwardSequence    = [forwardSequence copy];
    nodeCopy.backSequence       = [backSequence copy];

    return nodeCopy;
}

@end


//
//  Position.h
//

#import <Foundation/Foundation.h>

@class Sequence;

@interface Position : NSObject <NSCopying> {
    NSInteger Id;
    Sequence *leftSequence;
    Sequence *rightSequence;
}

@property (nonatomic, assign) NSInteger Id;
@property (nonatomic, retain) Sequence *leftSequence;
@property (nonatomic, retain) Sequence *rightSequence;

-(id) copyWithZone: (NSZone *) zone;

@end

//
//  Position.m
//

#import "Sequence.h"
#import "Position.h"

@implementation Position

@synthesize Id;
@synthesize leftSequence;
@synthesize rightSequence;

-(id) copyWithZone: (NSZone *) zone {
    Position *positionCopy = [[Position allocWithZone: zone] init];

    positionCopy.Id             = Id;
    positionCopy.leftSequence   = [leftSequence copy];
    positionCopy.rightSequence  = [rightSequence copy];

    return positionCopy;
}

@end

//
//  Sequence.h
//

#import <Foundation/Foundation.h>

@interface Sequence : NSObject <NSCopying> {
    NSInteger numberOfFrames;
    NSString *imageNameScheme;
    NSString *endFrame;
}

-(id) copyWithZone: (NSZone *) zone;

@property (nonatomic, assign) NSInteger numberOfFrames;
@property (nonatomic, copy) NSString *imageNameScheme;
@property (nonatomic, copy) NSString *endFrame;

@end

//
//  Sequence.m
//  MCIT
//

#import "Sequence.h"

@implementation Sequence

@synthesize numberOfFrames;
@synthesize imageNameScheme;
@synthesize endFrame;

-(id) copyWithZone: (NSZone *) zone {
    Sequence *sequenceCopy = [[Sequence allocWithZone: zone] init];

    sequenceCopy.numberOfFrames     = numberOfFrames;
    sequenceCopy.imageNameScheme    = imageNameScheme;
    sequenceCopy.endFrame           = endFrame;

    return sequenceCopy;
}
@end

Works like a charm now thanks all. :D

2
  • I'm not sure I understand your question. 1) What do you mean by "block copy" in this context? It looks like a normal copy method. 2) What are you asking, with regards to the code you posted? Are you having some problems? Commented Mar 1, 2011 at 21:29
  • I am forced to agree with @Chuck. You need to better explain your problem. All we know now is that you are trying to provide a copyWithZone: method. Commented Mar 1, 2011 at 21:37

1 Answer 1

2

If your intent is to make this a copyable class, then you need to declare that it conforms to the NSCopying protocol like so:

@interface Node: NSObject <NSCopying> {

Falling to declare the protocol can cause other objects to believe that the class is uncopyable even if it has a copyWithZone: method.

Sign up to request clarification or add additional context in comments.

2 Comments

This is something I definitely needed to do but it doesn't appear to be the final answer. :(
This fixed it the debugger couldn't grab the values after I added the <NSCopying> for some reason.

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.