gpt4 book ai didi

objective-c - iOS – NSMutableArray 和 block 复制

转载 作者:行者123 更新时间:2023-11-28 23:17:49 25 4
gpt4 key购买 nike

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

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

我知道代码没有正确发布……为了您的利益,我试着让代码更小。

任何你能分享的见解都会很棒。

//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 协议(protocol):

@interface Node: NSObject <NSCopying> {

未能声明协议(protocol)会导致其他对象认为该类不可复制,即使它具有 copyWithZone: 方法也是如此。

关于objective-c - iOS – NSMutableArray 和 block 复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5160392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com