Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 756
andyvn22 Avatar asked Apr 04 '10 02:04

andyvn22


People also ask

Why use NSCopying?

NSCopying can be used in Swift as a generic way to create copies of classes (reference types), and as a bonus, making a Swift type inherit from NSCopying will also allow it to make use of the copy property accessor in Objective-C bridges.

What is NSCopying?

A protocol that objects adopt to provide functional copies of themselves.


1 Answers

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.

like image 50
psychotik Avatar answered Oct 07 '22 16:10

psychotik