क्या कॉपीविथज़ोन (नीचे देखें) सही है, विशेष रूप से बिट जहां मैं सेटर्स का उपयोग नई ऑब्जेक्ट के इंस्टेंस चर को पॉप्युलेट करने के लिए करता हूं?copyWithZone सेटिंग आवृत्ति चर?
@interface Planet : NSObject <NSCopying>
{
NSString *name;
NSString *type;
NSNumber *mass;
int index;
}
@property(copy) NSString *name;
@property(copy) NSString *type;
@property(retain) NSNumber *mass;
@property(assign) int index;
-(void)display;
@end
-(id) copyWithZone: (NSZone *) zone {
Planet *newPlanet = [[Planet allocWithZone:zone] init];
NSLog(@"_copy: %@", [newPlanet self]);
[newPlanet setName:name];
[newPlanet setType:type];
[newPlanet setMass:mass];
[newPlanet setIndex:index];
return(newPlanet);
}
EDIT_001:
यह एक बेहतर तरीका है?
-(id) copyWithZone: (NSZone *) zone {
Planet *newPlanet = [[[self class] allocWithZone:zone] init];
[newPlanet setName:[self name]];
[newPlanet setType:[self type]];
[newPlanet setMass:[self mass]];
[newPlanet setIndex:[self index]];
return(newPlanet);
}
बहुत धन्यवाद
गैरी
वे दोनों काम करते हैं, लेकिन दूसरे तरीके से अनावश्यक विधि आमंत्रण है। – NSResponder
@NSResponder: मुझे लगता है कि अगर आप वापसी से पहले लिखते हैं (newPlanet); कुछ [self setMass: a_new_number] की तरह, आप newPlanet.mass मान को बदल देंगे, क्योंकि इसे बनाए रखा गया है। यही है ना ? – Oliver