[英]mutableCopyWithZone, copy NSMutableArray?
我試圖制作一個包含5個實例變量的行星對象的mutableCopy(其中一個是字符串文字的NSMutableArray。我的問題是我不確定如何將newPlanet> data設置為self> 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 *data; HOW TO: newPlanet>data = self>data?
return(newPlanet);
}
基於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];
你可能想要
newPlanet->data = [data mutableCopyWithZone:zone];
請注意,如果使用設置器(例如dot-access或setData:
,則將觸發另一個保留,因此您應該適當地處理它。
您是否嘗試過類似[newPlanet setData:[[self data] mutableCopyWithZone:zone]];
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.