簡體   English   中英

iOS - NSMutableArray和Block Copy

[英]iOS – NSMutableArray and Block Copy

目前我正在嘗試使用我當前的項目來獲取塊副本。 副本的結構是一個NSMutableArray,它包含NSIntegers,NSStrings另一個NSMutableArray和兩個對象......這些對象依次包含NSStrings。 該數組包含持有NSInteger的對象和包含字符串的兩個對象......

我相信我應該使用Block Copy方法來處理對象......代碼如下......

我知道代碼沒有正確發布...我試圖使代碼更小為您的利益。

你能擺脫的任何見解都會很棒。

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

如果你的目的是使它成為一個可復制的類,那么你需要聲明它符合NSCopying協議,如下所示:

@interface Node: NSObject <NSCopying> {

聲明協議可能導致其他對象認為該類是不可復制的,即使它具有copyWithZone:方法也是如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM