nscopying

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

一个人想着一个人 提交于 2019-12-05 11:20:19
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 the same storage by retaining the original and avoid the overhead of copying. However, what will happen if there is a mutable subclass? With a clean architecture, where a subclass does not have to care about implementation details of its base class,

Why zone is always nil while implementing NSCopying?

假如想象 提交于 2019-12-04 17:28:23
问题 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]]; 回答1: Kevin's and Robin's answer is the most accurate. Oscar's answer is pretty close to correct. But neither the Gnustep documentation nor logancautrell's reasons for the existence of

make UIImage conform to the NSCopying protocol

你。 提交于 2019-12-04 10:00:38
The question is quite simple, I need to have an UIImage conform to NSCopying protocol but I have absolutely no idea on where to start to achieve this. Do you have any pointer to help me? Thanks in advance - (id) copyWithZone: (NSZone *) zone { return [[UIImage allocWithZone: zone] initWithCGImage: self.CGImage]; } 来源: https://stackoverflow.com/questions/2081177/make-uiimage-conform-to-the-nscopying-protocol

When is NSCopying needed?

好久不见. 提交于 2019-12-04 02:13:26
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? When it's being passed to a copy property or any other method that is documented as copying its argument. Think of the NSCopying protocol as the objective-C version of cloning routines. If a caller was to clone your object, what is the behavior you would want? If your object is solely composed of primitive types, then you don't need to worry about this. But if you have

iPhone : (id)copyWithZone:(NSZone *)zone : what is “zone” for?

狂风中的少年 提交于 2019-12-03 11:20:22
When implementing this method of NSCopying in a class to enable copy, what is the zone param use ? If I set a new object, I do not need to alloc it with allocWithZone as an alloc is just enough... I'm confused... Lily Ballard It's a relic from the old days, where we had multiple "zones" to allocate in. These days, all apps only have a single zone where all allocations are made, but the NSZone class still exists and far too much code is written to depend on +allocWithZone: being the fundamental allocation method to make the change. In short, you can ignore the NSZone struct in its entirety, and

Why zone is always nil while implementing NSCopying?

守給你的承諾、 提交于 2019-12-03 10:32:09
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]]; Kevin's and Robin's answer is the most accurate. Oscar's answer is pretty close to correct. But neither the Gnustep documentation nor logancautrell's reasons for the existence of zones is quite correct. Zones were originally created -- first NXZone, then NSZone -- to ensure that objects

UIView as dictionary key?

旧街凉风 提交于 2019-12-03 09:29:50
I want to have a NSDictionary that maps from UIView s to something else. However, since UIViews do not implement the NSCopying protocol, I can't use them directly as dictionary keys. You can use an NSValue holding the pointer to the UIView and use this as key. NSValues are copyable. but, if the view is destroyed, the NSValue will hold a junk pointer. Rok Strniša Here is the actual code (based on the answer by luvieere and further suggestion by Yar): // create dictionary NSMutableDictionary* dict = [NSMutableDictionary new]; // set value UIView* view = [UILabel new]; dict[[NSValue

NSManagedObject as NSDictionary key?

和自甴很熟 提交于 2019-11-30 11:07:53
In my app, I have a NSDictionary whose keys should be instances of a subclass of NSManagedObject . The problem, however, is that NSManagedObject does not implement the NSCopying protocol which means that no Core Data objects / instances of NSManagedObject can be used as dictionary keys even though the -[hash] method works fine for them. Was should I do? There are four options: Use a different object as the dictionary key instead, and lookup from that. [object objectID] or +[NSValue valueWithNonretainedObject:] seem the most obvious Use CFDictionaryCreateMutable() to create a dictionary with

Implementing NSCopying in Subclass of Subclass

余生颓废 提交于 2019-11-30 08:06:19
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 a "width" int. The copyWithZone: methods are seen below: Shape - (id)copyWithZone:(NSZone *)zone { Shape *s = [[Shape alloc] init]; s.sides = self.sides; return s; } Square - (id)copyWithZone:(NSZone *)zone { Square *s = (Square *)[super copyWithZone:zone]; s

NSManagedObject as NSDictionary key?

天涯浪子 提交于 2019-11-29 16:35:55
问题 In my app, I have a NSDictionary whose keys should be instances of a subclass of NSManagedObject . The problem, however, is that NSManagedObject does not implement the NSCopying protocol which means that no Core Data objects / instances of NSManagedObject can be used as dictionary keys even though the -[hash] method works fine for them. Was should I do? 回答1: There are four options: Use a different object as the dictionary key instead, and lookup from that. [object objectID] or +[NSValue