문제

Does the Objective-c copy method do a deep copy of an object?

NSObject *clone = [self copy];
도움이 되었습니까?

해결책

NSObject *clone = [self copy];

Wont perform deep copy but would be a shallow copy.

You need to implement NSCopying protocol like this

//ContactCard.h
@interface ContactCard : NSObject<NSCopying>
{
    NSString* name;
    NSString* email;
}
...
-(id)copyWithZone:(NSZone *)zone;
@end

//ContactCard.m
@implementation ContactCard
...
-(id)copyWithZone:(NSZone *)zone
{
    ContactCard* cc = [[ContactCard allocWithZone:zone]init];
    cc->email = [email copy];
    cc->name = [name copy];
    return cc;
}
...
@end

You can read more on enter link description here Or refer Apple docs on NSCopying

다른 팁

this might be help. For deep copy of any custom object we need to adopt NSCopying protocol in that class. How to copy an object in objective c

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top