2011-03-01 58 views
0

目前我正试图获得当前项目的块副本的挂起。该副本的结构是一个NSMutableArray,它包含NSIntegers,NSStrings另一个NSMutableArray和两个对象...这些对象又保存NSStrings。该数组包含持有一个NSInteger和包含字符串的两个对象...iOS - NSMutableArray和块复制

我相信我应该使用的块复制方法进行应对对象的对象...代码如下...

我知道代码没有释放妥善......我试着让代码变得更小以利于您的利益。

任何你可以摆脱的见解都会很棒。

//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 

现在感谢所有工作。 :d

+0

我不确定我理解你的问题。 1)在这种情况下,“块复制”是什么意思?它看起来像一个正常的复制方法。 2)关于你发布的代码,你在问什么?你有问题吗? – Chuck 2011-03-01 21:29:15

+0

我被迫同意@Chuck。你需要更好地解释你的问题。我们现在知道的是,您正在尝试提供copyWithZone:方法。 – TechZen 2011-03-01 21:37:12

回答

2

如果你的目的是使之成为一个可复制类,那么你需要声明它符合NSCopying协议,像这样:

@interface Node: NSObject <NSCopying> { 

落申报协议可能会导致其他对象相信该类即使具有copyWithZone:方法也是不可复制的。

+0

这是我绝对需要做的事情,但它似乎并不是最终的答案。 :( – Mytheral 2011-03-02 12:53:11

+0

这固定它调试器无法抓取值,因为某些原因我添加了。 – Mytheral 2011-03-02 13:51:10