iOS ߝ NSMutableArray 和块复制

发布于 2024-10-19 23:18:55 字数 3768 浏览 3 评论 0原文

目前,我正在尝试掌握当前项目的块副本的窍门。副本的结构是一个 NSMutableArray,其中包含 NSIntegers、NSStrings、另一个 NSMutableArray 和两个对象……这些对象依次保存 NSStrings。该数组包含持有 NSInteger 的对象和两个包含字符串的对象...

我相信我应该使用块复制方法来处理对象...代码如下...

我知道代码没有正确发布...我尝试编写代码为了您的利益而缩小。

你能提供的任何见解都会很棒。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2024-10-26 23:18:55

如果您的目的是使其成为可复制的类,那么您需要声明它符合 NSCopying 协议,如下所示:

@interface Node: NSObject <NSCopying> {

声明协议可能会导致其他对象认为该类是不可复制的如果它有一个 copyWithZone: 方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文