gpt4 book ai didi

objective-c - mutableCopyWithZone,复制 NSMutableArray?

转载 作者:搜寻专家 更新时间:2023-10-30 19:57:25 25 4
gpt4 key购买 nike

我试图制作一个包含 5 个实例变量的行星对象的 mutableCopy(其中一个是字符串文字的 NSMutableArray。我的问题是我不确定如何将 newPlanet>data 设置为 self> 的副本数据,这有意义吗?

-(id) mutableCopyWithZone: (NSZone *) zone {
Planet *newPlanet = [[Planet allocWithZone:zone] init];
NSLog(@"_mutableCopy: %@", [newPlanet self]);
[newPlanet setName:name];
[newPlanet setType:type];
[newPlanet setMass:mass];
[newPlanet setIndex:index];

// NSMutableArray *data; HOW TO: newPlanet>data = self>data?

return(newPlanet);
}

EDIT_001:

根据 Chuck 和 bbum 的评论,我更新了我的方法并添加了以下内容......

@property(retain) NSMutableArray *data;
@synthesize data;

.

-(id) mutableCopyWithZone: (NSZone *) zone {
Planet *newPlanet = [[Planet allocWithZone:zone] init];
NSLog(@"_mutableCopy: %@", [newPlanet self]);
[newPlanet setName:name];
[newPlanet setType:type];
[newPlanet setMass:mass];
[newPlanet setIndex:index];

NSMutableArray *copiedArray = [[self data] mutableCopyWithZone:zone];
[newPlanet setData: copiedArray];
[copiedArray release];

return(newPlanet);
}

非常感谢

加里

最佳答案

这里没什么特别的。像这样:

NSMutableArray *copiedData = [[self data] mutableCopyWithZone:zone];
newPlanet.data = copiedData;
[copiedData release];

关于objective-c - mutableCopyWithZone,复制 NSMutableArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2015943/

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