2

copyWithZone を実装するための 2 つの少し異なる方法を見てきました。彼らは違うのですか?

最初のコード:

@interface Person : NSObject <NSCopying>

@property (nonatomic,weak) NSString *name;
@property (nonatomic,weak) NSString *surname;
@property (nonatomic,weak) NSString *age;

-(id) copyWithZone:(NSZone *)zone;

@end

//and 1st implementation
@implementation Person
-(id) copyWithZone:(NSZone *)zone
{
Person *copy = [[self class] allocWithZone:zone]; 
copy.name = self.name; //should we use copy.name=[self.name copyWithZone: zone]; ?
copy.age = self.age;
copy.surname = self.surname; //here we make assignment or copying?
return copy;
}
@end

そして2番目のコード

@interface YourClass : NSObject <NSCopying> 
{
   SomeOtherObject *obj;
}

// In the implementation
-(id)copyWithZone:(NSZone *)zone
{
  // We'll ignore the zone for now
  YourClass *another = [[YourClass alloc] init];
  another.obj = [obj copyWithZone: zone];

  return another;
}

それらが異なる場合、どちらがより正しいですか? 1. 1stの【セルフクラス】と混同しました。Objective C のメタクラスについては知っていますが、そのコードのチャンクで必要ですか? 2. copy.name = self.name; vs another.obj = [obj copyWithZone: zone] 上記の違いは何ですか?

4

1 に答える 1