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
86
votes
3 answers

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 …
user440096
66
votes
8 answers

How do copy and mutableCopy apply to NSArray and NSMutableArray?

What is the difference between copy and mutableCopy when used on either an NSArray or an NSMutableArray? This is my understanding; is it correct? // ** NSArray ** NSArray *myArray_imu = [NSArray arrayWithObjects:@"abc", @"def", nil]; // No copy,…
fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
27
votes
7 answers

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.
mrueg
  • 8,185
  • 4
  • 44
  • 66
23
votes
1 answer

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 :…
Craig Otis
  • 31,257
  • 32
  • 136
  • 234
21
votes
4 answers

NSManagedObject as NSDictionary key?

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…
mrueg
  • 8,185
  • 4
  • 44
  • 66
16
votes
2 answers

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

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...
Oliver
  • 23,072
  • 33
  • 138
  • 230
15
votes
5 answers

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…
Marcin
  • 3,694
  • 5
  • 32
  • 52
12
votes
3 answers

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?
andyvn22
  • 14,696
  • 1
  • 52
  • 74
10
votes
3 answers

Best practice for copying private instance vars with NSCopying

I might be missing something obvious here, but I'm implementing NSCopying on one of my objects. That object has private instance variables that are not exposed via getters, as they shouldn't be used outside the object. In my implementation of…
Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
9
votes
2 answers

Using instancetype as the return type of a copy in Objective-C?

Using instancetype as a return value of init and related methods is the recommended way to proceed, see the latest clang features. However, what is the best practice w.r.t. the return value of copyWithZone: in the NSCopying protocol (see this thread…
user8472
  • 3,268
  • 3
  • 35
  • 62
8
votes
2 answers

Override copy or copyWithZone: or both?

I'm confused looking at Apple's documentation and reading through Cocoa design patterns. In the Apple documentation for copyWithZone:, it reads: This method exists so class objects can be used in situations where you need an object that conforms…
Crystal
  • 28,460
  • 62
  • 219
  • 393
8
votes
2 answers

What is the difference between "-copy" and "-copyWithZone:"?

I know that copy creates an immutable copy of an object but i just want to know how copywithzone works and what is the basic difference between copy and copywithzone
prakhar
  • 828
  • 7
  • 18
7
votes
1 answer

Date.copy() in Swift 3.0

Since the switch to Swift 3.0, and along with it the change of NSDate to Date, the class no longer conforms to the NSCopying protocol. In Swift 2, this was valid: let newDate = oldDate.copy() But now returns a compiler error. With this being the…
JoGoFo
  • 1,928
  • 14
  • 31
6
votes
1 answer

What is the meaning of "zone" in copyWithZone:?

I was going through "Pro. Objective-C Design Patterns for iOS" by Chung and found _sharedSinglton = [[super allocWithZone: NULL] init]; I looked in Apple's documentation for NSCopying as well, but can't really understand what a ZONE really means.
Prashant Rane
  • 436
  • 4
  • 11
6
votes
3 answers

NSCopying copy(with:) - Does it really need to return Any?

Is there any way to use NSCopying without the returned object being of type Any? It always forces me to cast. This seems strange. I'm copying the object, shouldn't Swift know it's the same type by definition of the word copy? Is there another way to…
MH175
  • 2,234
  • 1
  • 19
  • 35
1
2 3 4 5 6 7