Reputation: 29767
I need to deep copy a custom object that has objects of its own. I've been reading around and am a bit confused as to how to inherit NSCopying and how to use NSCopyObject.
Upvotes: 117
Views: 120430
Reputation: 310
Has it's limitations: works almost only with NS-friendly properties, a class must have @objcMembers
public protocol NSObjectCopier {
func makeCopy<T: NSObject>(of source: T) -> T
}
struct StandartNSObjectCopier: NSObjectCopier {
func makeCopy<T>(of source: T) -> T where T : NSObject {
var copy = T.init()
var numOfProperties: UInt32 = 0
let properties = class_copyPropertyList(T.self, &numOfProperties)
for i in 0..<numOfProperties {
let idx = Int(i)
let prop = properties![idx]
let rawAttrs = String(cString: property_getAttributes(prop)!)
let attrs = Set(rawAttrs.components(separatedBy: ","))
if attrs.contains("R") {
//skip readonly
continue
}
let nsKey = NSString(utf8String: property_getName(prop))!
let key = nsKey as String
let srcValue = source.value(forKey: key)
copy.setValue(srcValue, forKey: key)
}
return copy
}
}
Upvotes: 0
Reputation: 7588
This is probably unpopular way. But here how I do it:
object1 = // object to copy
YourClass *object2 = [[YourClass alloc] init];
object2.property1 = object1.property1;
object2.property2 = object1.property2;
..
etc.
Quite simple and straight forward. :P
Upvotes: -2
Reputation: 440
I don't know the difference between that code and mine, but I have problems with that solution, so I read a little bit more and found that we have to set the object before return it. I mean something like:
#import <Foundation/Foundation.h>
@interface YourObject : NSObject <NSCopying>
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *line;
@property (strong, nonatomic) NSMutableString *tags;
@property (strong, nonatomic) NSString *htmlSource;
@property (strong, nonatomic) NSMutableString *obj;
-(id) copyWithZone: (NSZone *) zone;
@end
@implementation YourObject
-(id) copyWithZone: (NSZone *) zone
{
YourObject *copy = [[YourObject allocWithZone: zone] init];
[copy setNombre: self.name];
[copy setLinea: self.line];
[copy setTags: self.tags];
[copy setHtmlSource: self.htmlSource];
return copy;
}
I added this answer because I have a lot of problems with this issue and I have no clue about why is it happening. I don't know the difference, but it's working for me and maybe it can be useful for others too : )
Upvotes: 23
Reputation: 19446
There is also the use of the -> operator for copying. For Example:
-(id)copyWithZone:(NSZone*)zone
{
MYClass* copy = [MYClass new];
copy->_property1 = self->_property1;
...
copy->_propertyN = self->_propertyN;
return copy;
}
The reasoning here is the resulting copied object should reflect the state of the original object. The "." operator could introduce side effects as this one calls getters which in turn may contain logic.
Upvotes: 0
Reputation: 49376
As always with reference types, there are two notions of "copy". I'm sure you know them, but for completeness.
You want the latter. If this is one of your own objects, you need simply adopt the protocol NSCopying and implement -(id)copyWithZone:(NSZone *)zone. You're free to do whatever you want; though the idea is you make a real copy of yourself and return it. You call copyWithZone on all your fields, to make a deep copy. A simple example is
@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;
}
Upvotes: 193
Reputation: 713
another.obj = [obj copyWithZone: zone];
I think, that this line causes memory leak, because you access to obj
through property which is (I assume) declared as retain
. So, retain count will be increased by property and copyWithZone
.
I believe it should be:
another.obj = [[obj copyWithZone: zone] autorelease];
or:
SomeOtherObject *temp = [obj copyWithZone: zone];
another.obj = temp;
[temp release];
Upvotes: 3
Reputation: 2795
Apple documentation says
A subclass version of the copyWithZone: method should send the message to super first, to incorporate its implementation, unless the subclass descends directly from NSObject.
to add to the existing answer
@interface YourClass : NSObject <NSCopying>
{
SomeOtherObject *obj;
}
// In the implementation
-(id)copyWithZone:(NSZone *)zone
{
YourClass *another = [super copyWithZone:zone];
another.obj = [obj copyWithZone: zone];
return another;
}
Upvotes: 26