nscopying

Using -mutableCopyWithZone: on custom class makes it immutable

£可爱£侵袭症+ 提交于 2020-01-05 06:54:27
问题 I've created a custom class which conforms to NSCopying and NSMutableCopying . I've added an implementation for -copyWithZone: and -mutableCopyWithZone: , but whenever I call -mutableCopy on my object and try to call another method, it crashes because some of the ivars have become immutable, even though I call -mutableCopyWithZone: on the ivars. Here's how I'm copying my class: MyObject *flipped = [list mutableCopy]; [MyObject flip:flipped]; (the code fails on +flip: , because it tries to use

Why doesn't UIView (or it's subclasses) adopt the NSCopying Protocol?

隐身守侯 提交于 2020-01-02 07:35:09
问题 Can a Cocoahead please explain why UIView and it's sub classes don't adopt the NSCopying Protocol? I can see, philosophically, why UITouch would not be copy compliant, as it's a very temporal object. By UIView, and it's subclasses, especially UIButton, seem like they should be able to be copied. Certainly Apple has good reasons for doing things the way they do. Do you know their reason? 回答1: It would seem the question they asked wasn't "Why not?" but "Why do it?" There's little point to doing

Implementing NSCopying in Subclass of Subclass

一笑奈何 提交于 2019-12-30 02:38:05
问题 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

UIView as dictionary key?

雨燕双飞 提交于 2019-12-21 03:25:15
问题 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. 回答1: 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. 回答2: Here is the actual code (based on the answer by luvieere and further suggestion by Yar): // create dictionary NSMutableDictionary*

Implementing NSCopying

心已入冬 提交于 2019-12-17 03:01:11
问题 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 has an array of objects called

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

丶灬走出姿态 提交于 2019-12-09 08:45:57
问题 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... 回答1: 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

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

给你一囗甜甜゛ 提交于 2019-12-07 06:46:03
问题 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

make UIImage conform to the NSCopying protocol

China☆狼群 提交于 2019-12-06 06:20:22
问题 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 回答1: - (id) copyWithZone: (NSZone *) zone { return [[UIImage allocWithZone: zone] initWithCGImage: self.CGImage]; } 来源: https://stackoverflow.com/questions/2081177/make-uiimage-conform-to-the-nscopying-protocol

Why doesn't UIView (or it's subclasses) adopt the NSCopying Protocol?

送分小仙女□ 提交于 2019-12-05 23:43:51
Can a Cocoahead please explain why UIView and it's sub classes don't adopt the NSCopying Protocol? I can see, philosophically, why UITouch would not be copy compliant, as it's a very temporal object. By UIView, and it's subclasses, especially UIButton, seem like they should be able to be copied. Certainly Apple has good reasons for doing things the way they do. Do you know their reason? It would seem the question they asked wasn't "Why not?" but "Why do it?" There's little point to doing so. There's rarely a need to copy a live view. Usually, template views are created through the NSCoding

When is NSCopying needed?

余生长醉 提交于 2019-12-05 14:57:55
问题 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? 回答1: When it's being passed to a copy property or any other method that is documented as copying its argument. 回答2: 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?