how to do true deep copy for NSArray and NSDictionary with have nested arrays/dictionary?

后端 未结 4 1985
一整个雨季
一整个雨季 2020-11-27 03:36

Question: Is there a way to use existing objective-c methods to do a full deep copy of a NSDictionary or NSArray, that themselves have nested dictionaries or arrays within t

4条回答
  •  执念已碎
    2020-11-27 04:06

    I think something like this should work.

    NSData *buffer;
    NSMutableDictionary *_dict1, *_dict2;
    
    // Deep copy "all" objects in _dict1 pointers and all to _dict2  
    buffer = [NSKeyedArchiver archivedDataWithRootObject: _dict1];  
    _dict2 = [NSKeyedUnarchiver unarchiveObjectWithData: buffer];  
    

提交回复
热议问题