When is NSCopying needed?
Asked Answered
I

3

12

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?

Indebtedness answered 4/4, 2010 at 2:15 Comment(0)
S
3

When it's being passed to a copy property or any other method that is documented as copying its argument.

Shellacking answered 4/4, 2010 at 2:19 Comment(0)
K
7

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 any complex types as members, you might want to clone/copy them instead of passing simple references to them.

Kannan answered 4/4, 2010 at 2:23 Comment(0)
M
4

NSCopying has quite broad usage. Anytime an object owns something it shouldn't share with other objects, you'll need a copy. Mutable objects often need to be copied, though NSMutableCopying might be needed instead of NSCopying.

Mcpeak answered 4/4, 2010 at 2:20 Comment(0)
S
3

When it's being passed to a copy property or any other method that is documented as copying its argument.

Shellacking answered 4/4, 2010 at 2:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.