Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a copy of a NSObject

How do I take a NSObject that I have set all the attributes of and then copy that into another block of memory that an array can use so I can re use the original one?

like image 658
Chris Avatar asked Sep 13 '10 01:09

Chris


People also ask

How do I create an NSObject in Objective-C?

Creating a Custom ClassGo ahead an choose “Objective-C class” and hit Next. For the class name, enter “Person.” Make it a subclass of NSObject. NSObject is the base class for all objects in Apple's developer environment. It provides basic properties and functions for memory management and allocation.

What is copy Objective-C?

Copy is useful when you do not want the value that you receive to get changed without you knowing. For example if you have a property that is an NSString and you rely on that string not changing once it is set then you need to use copy.

What does NSObject mean?

The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.


1 Answers

In short, you don't.

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

If you are asking how you copy an object into, say, a random malloc() block somewhere -- say, in the middle of an array -- then that is a very different issue. It can technically be done, but basically no one does so as the frameworks and runtime aren't designed for that.

Without knowing more about your specific needs, it is impossible to go into more detail.

like image 98
bbum Avatar answered Sep 20 '22 15:09

bbum