Create a copy of NSObject

How can I take the NSObject that I set for all the attributes and then copy them to another block of memory that the array can use so that I can use the original?

+7
objective-c iphone copy nsarray
source share
2 answers

In short, you are not doing this.

If you want to put an object in an array and then create a new object, do just that; addObject: to the array and alloc/init new.

If you ask how you copy an object into, say, a random malloc() block somewhere - say, in the middle of an array - then this is a completely different problem. This can be technically done, but basically no one does it so that the framework and runtime are not designed for this.

Without knowing more about your specific needs, it is impossible to dwell in more detail.

+7
source share

Creating a copy of the object is done by sending a copy message. This only works on instances of classes that implement the NSCopying protocol.

Read Copy Implementation for a good overview. Then read Implementing NSCopying is considered harmful for more information.

+7
source share

All Articles