Using the Archiver to Copy Objects
In Program 18.2, you tried to make a copy of an array containing mutable string elements and you saw how a shallow copy of the array was made. That is, the actual strings themselves were not copied—only the references to them were copied.
You can use the Foundation’s archiving capabilities to create a deep copy of an object. For example, Program 19.11 copies dataArray to dataArray2 by archiving dataArray into a buffer and then unarchiving it, assigning the result to dataArray2. You don’t need to use a file for this process; the archiving and unarchiving process can all take place in memory.
Program 19.11
#import <Foundation/Foundation.h>int main (int argc, char * argv[]){ @autoreleasepool { NSData *data; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access