copyWithZone을 구현하는 데있어 두 가지 방법이 조금 다릅니다. 그들은 다른가요?딥 복사 - copyWithZone의 두 구현이 다른가요?
첫 번째 코드 :
@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. 1 일에 [자아 수업]과 혼동합니다. 객관적인 C에서 메타 클래스에 대해 알고 있지만, 코드 덩어리에서 필요합니까? 2. copy.name = self.name; vs another.obj = [obj copyWithZone : zone] 위와 다른 점은 무엇입니까?