Tag: nscopying

Is returning in copyWithZone for immutable classes with mutable subclasses really safe / a good idea?

One often reads, that immutable classes can implement copyWithZone very efficiently in the following way: – (id) copyWithZone:(NSZone*)zone { return [self retain]; } The idea behind that implementation is obvious: The original and the copy are both immutable instances and they will always have exactly the same content, so why not let both point to […]

When is NSCopying needed?

I know it’s needed if your object will be used as a key in an NSDictionary. Are there any other times like this that NSCopying is required? If I think I don’t need my model objects to conform to NSCopying, am I probably wrong?

UIView as dictionary key?

I want to have a NSDictionary that maps from UIViews to something else. However, since UIViews do not implement the NSCopying protocol, I can’t use them directly as dictionary keys.

Implementing NSCopying in Subclass of Subclass

I have a small class hierarchy that I’m having trouble implementing copyWithZone: for. I’ve read the NSCopying documentation, and I can’t find the correct answer. Take two classes: Shape and Square. Square is defined as: @interface Square : Shape No surprise there. Each class has one property, Shape has a “sides” int, and Square has […]

Why zone is always nil while implementing NSCopying?

It may be simple question, but why implementing NSCopying protocol in my class, I get zone == nil – (id)copyWithZone:(NSZone *)zone { if (zone == nil) NSLog(@”why this is allways nil”); (…) } This is called using this method for copy array with objects. [[NSArray alloc] initWithArray:myArray copyItems:YES]];

Implementing NSCopying

I’ve read the NSCopying docs but I am still very unsure about how to implement what is required. My class Vendor: @interface Vendor : NSObject { NSString *vendorID; NSMutableArray *availableCars; BOOL atAirport; } @property (nonatomic, copy) NSString *vendorID; @property (nonatomic, retain) NSMutableArray *availableCars; @property (nonatomic, assign) BOOL atAirport; – (id)initFromVehVendorAvailsDictionary:(NSDictionary *)vehVendorAvails; @end The Vendor class […]