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

swift How to implement the copy method more simply?

I have created multiple classes, all of which need to implement the NSCopying protocol, but there are a lot of properties in my classes, is there an easier way? Below is my current way: class TestA: NSObject, NSCopying { var a: CGFloat = 0 …
quan
  • 49
  • 3
2
votes
1 answer

why do I get "Attempted to unregister unknown __weak variable" when copying an instance variable?

I noticed this today when playing with NSOutlineView and NSTableHeaderCell, but when this specific configuration is made, an error/warning(?) is printed: objc[2774]: Attempted to unregister unknown __weak variable at 0x1016070d0. This is probably…
gietal
  • 129
  • 10
2
votes
1 answer

What sense to copy empty array?

I were reading another programmer's code, so i found this: @property(nonatomic, strong) NSArray *assets; ---- _assets = [@[] mutableCopy]; __block NSMutableArray *tmpAssets = [@[] mutableCopy]; Is it some kind of trick? Why does he used…
Zaporozhchenko Oleksandr
  • 4,660
  • 3
  • 26
  • 48
2
votes
1 answer

Proper way to copy a readonly NSMutableArray

I have an object with a readonly property that I am trying to implement NSCopying for. It has a mutableArray called "subConditions" (which holds "SubCondition" objects). I have made it readonly because I want callers to be able to change the data in…
Jon Hull
  • 679
  • 6
  • 15
2
votes
2 answers

To be a key of an NSDictionary, must a class also implement isEqual: and hash?

I understand that a class must implement NSCopying in order to be a key of an NSDictionary, but is implementing isEqual: and hash also necessary or advisable? If yes, why?
cfischer
  • 24,452
  • 37
  • 131
  • 214
2
votes
1 answer

Any reason to not use existing NSCoding methods to implement NSCopying

Is there a reason that given a class that implements NSCoding that the implementation of copyWithZone: shouldn't be implemented using this pattern: -(instancetype)copyWithZone:(NSZone *)zone{ return [NSKeyedUnarchiver…
Jon
  • 462
  • 4
  • 13
2
votes
0 answers

How to make a class instance in Swift copyable?

In Objective-C, you can make your class instance copyable by implementing NSCopying and copyWithZone:. How do you do similar thing in Swift (without using Foundation)?
Boon
  • 40,656
  • 60
  • 209
  • 315
2
votes
1 answer

no known instance method for selector 'copy' on protocol conforming to NSCopying and NSObject

When I compile the code below the line id copiedData = [_localData copy]; results in the compiler error "no known instance method for selector 'copy'." Given that _localData is of type id and given that IGTestClassData conforms to…
HairOfTheDog
  • 2,489
  • 2
  • 29
  • 35
2
votes
3 answers

Can I use NSPredicate as key in a NSDictionary

Say I have two NSPredicates NSPredicate *pa = [NSPredicate predicateWithBlock:^(BOOL)(id evaluatedObject, NSDictionary *bindings) { return [evaluatedObject isKindOfClass:[TestClass class]]; }]; NSPredicate *pb = [NSPredicate…
L__
  • 866
  • 1
  • 9
  • 15
2
votes
1 answer

Using NSCopy to Copy a Custom Object Containing Pointers?

I am learning how to use NSCopy. I want to make a copy of a custom object I am using, which is an ImageView in a UIScrollView. I am trying to implement NSCopying protocol as follows : -(id)copyWithZone:(NSZone *)zone { ImageView *another =…
GuybrushThreepwood
  • 5,598
  • 9
  • 55
  • 113
2
votes
0 answers

Is this a reasonable pattern for implementing NSMutableCopying?

I'm looking to implement the NSMutableCopying interface as I have a set of objects which are immutable, but I also need to be able to create modified copies. Let's assume we've got a movie defined something like this (of course, really there would…
Greg Beech
  • 133,383
  • 43
  • 204
  • 250
2
votes
2 answers

copyWithZone issue

I am trying to make a copy of my UIViewController subclass by doing: BookViewController *bookVC = [catalogFlatViewController copy]; and I have the following error: '-[BookViewController copyWithZone:]: unrecognized selector sent to instance…
xonegirlz
  • 8,889
  • 19
  • 69
  • 127
1
vote
2 answers

NSCopying, copyWithZone and NSDictionary

Firstly I would like confirmation that I have understood NSCopying correctly ... In order to use a simple NSObject subclass as a key in an NSDictionary I must have it implement the NSCopying protocol. In the copied instance's copyWithZone method I…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
1
vote
1 answer

Does the different way of handling key between NSMutableDictionary and NSCache (copy vs. retain) result in different consequence?

I studied the differences between NSMutableDictionary and NSCache. One of them was that NSMutableDictionary copy its keys and NSCache just retain it as a strong reference. In addition, I found that the reason why the key is copied from the…
naljin
  • 439
  • 3
  • 10
1
vote
1 answer

NSCopy GKGameModel doesn't copy player objects correctly

I am trying to use swift NSCopy to do a deep copy of a GKGameModel's object, including all players and their wallet reference (containing Integers representing their cash). Using Swift playgrounds I try to credit all copied players with $100, but…
zardon
  • 1,601
  • 4
  • 23
  • 46