Questions tagged [nscopying]

NSCopying is a protocol that provides a way to make a copy of an object.

NSCopying is a protocol that allows a class to make a copy of itself in whatever manner makes sense for that object. The only requirement is to implement copyWithZone:, which typically duplicates any relevant instance variables into a new instance. Other classes can make a copy of an object by calling copy, which is implemented by NSObject to simply call copyWithZone: on the receiver.

Apple Documentation for NSCopying

103 questions
5
votes
1 answer

Is returning [self retain] 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…
Kaiserludi
  • 2,434
  • 2
  • 23
  • 41
5
votes
2 answers

How to create a copy of uiview (not a pointer to original uiview)

I want to create a copy of a UIView, and I dont want to use NSKeyedArchiver because I am frequently creating a copy of many views, and using NSKeyedArchiver was slow. I heard about copy or initWithZone:, but Googling it found it is not good for…
Karan Sehgal
  • 91
  • 1
  • 5
4
votes
1 answer

NSCopyObject considered harmful?

In the Xcode documentation for NSCopyObject, the special considerations section states: This function is dangerous and very difficult to use correctly. It's use as part of copyWithZone: by any class that can be subclassed, is highly error prone.…
Steve Madsen
  • 13,465
  • 4
  • 49
  • 67
4
votes
2 answers

Why must the key of an NSDictionary conform to NSCopying

I noticed that for an object to be a key for an NSDictionary it must conform to NSCopying. Why is this so? I would understand that all keys must implement hash, but why NSCopying?
cfischer
  • 24,452
  • 37
  • 131
  • 214
4
votes
2 answers

How to make a deep copy with copyWithZone to duplicate a structure?

I have a class that represents a structure. This class called Object has the following properties @property (nonatomic, strong) NSArray *children; @property (nonatomic, assign) NSInteger type; @property (nonatomic, strong) NSString *name; @property…
Duck
  • 34,902
  • 47
  • 248
  • 470
4
votes
1 answer

Objective-c: custom class instance as dictionary key returns nil when key exists

I have a custom class I'm using instances of as keys in a dictionary. The problem is, sometimes (not all the time) requesting a value from a dictionary with one of my custom instance keys returns nil when the key really is in the dictionary. I…
joxl
  • 287
  • 1
  • 4
  • 9
4
votes
1 answer

make UIImage conform to the NSCopying protocol

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
AP.
  • 5,205
  • 7
  • 50
  • 94
4
votes
1 answer

Enabling the NSCopying protocol in a class

I have a class that has been derived from NSObject. How can copy be enabled like [object copy]? This is for an iPhone application.
Lakshmie
  • 331
  • 6
  • 17
4
votes
2 answers

NSString property and custom init

I have 2 questions. First - Are string declared as such in obj-c @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *city; Are those (nonatomic, copy) right or should I use (nonatomic, strong), or something else…
Želja Huber
  • 337
  • 5
  • 14
4
votes
3 answers

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

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…
SooDesuNe
  • 9,880
  • 10
  • 57
  • 91
3
votes
1 answer

Should I check for nil in copyWithZone:?

In the Sketch example, in -[ copyWithZone:] is not checked if -[ init] returns nil: - (id)copyWithZone:(NSZone *)zone { SKTGraphic *copy = [[[self class] alloc] init]; copy->_bounds = _bounds; copy->_isDrawingFill =…
user142019
3
votes
2 answers

Objective-C pattern for creating mutable copies

I have many "model" objects whose properties are defined as "readonly" and shared among various components. In some cases I need to create local mutable copies of the objects (using them for local mutable state) I rather not implement NSMutableCopy…
Avba
  • 14,822
  • 20
  • 92
  • 192
3
votes
4 answers

Warning : "Sending 'NSObject *' to parameter of incompatible type 'id'

I am struck in this warning for many hours... Am getting The code is, -(NSMutableDictionary*)iOSDiction { NSMutableDictionary *tmpDic = [[NSMutableDictionary alloc] initWithCapacity:[m_coordDic count]]; for (NSObject…
AC Mohan
  • 33
  • 1
  • 4
3
votes
1 answer

Correctly NSCopying a "parent" property so it points to its already copied parent

I have a class A that contains a property of class B. Class B has a weak reference to its "parent" class A. Both classes implement NSCopying. I don't know how exactly NSCopying should be implemented in class B. I see the two obvious choices: assign…
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
2
votes
2 answers

Questions about duplicating last object of a NSArray

I've a NSArray of MyObjects. I want to duplicate the last object of my array. In other terms, I want to add a new object to the array that's exactly the same of the last one. I tried with: id object = [[self arrangedObjects] lastObject]; id…
aneuryzm
  • 63,052
  • 100
  • 273
  • 488