Search code examples
objective-cipadiosnsmutablearrayblock

iOS – NSMutableArray and Block Copy


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


Solution

  • 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.