2011-03-01 3 views
0

현재 현재 프로젝트에서 블록 복사본을 보려고합니다. 복사본의 구조는 NSIntegers, NSStrings 또 다른 NSMutableArray 및 두 개의 Object를 포함하는 NSMutableArray입니다. 이러한 객체는 차례대로 NSString을 보유합니다. 배열은 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

+0

@Chuck에 동의해야합니다. 문제를 더 잘 설명해야합니다. 이제 우리가 알고있는 것은 copyWithZone : 메소드를 제공하려고한다는 것입니다. – TechZen

답변

2

, 당신은 지금처럼 NSCopying 프로토콜로는 준수 선언해야합니다 프로토콜을 선언 떨어지는

@interface Node: NSObject <NSCopying> { 

믿고 다른 개체를 일으킬 수 있습니다 클래스가 copyWithZone: 메서드를 가지고 있어도 복사 할 수 없다.

+0

이것은 분명히해야 할 일이지만 최종 답안으로 보이지 않습니다. : – Mytheral

+0

이것은 어떤 이유로 든 을 추가 한 후에 디버거가 값을 가져올 수 없다는 것을 수정했습니다. – Mytheral