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

后端 未结 4 2032
一整个雨季
一整个雨季 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:26

    The easy way for a DEEP copy is to simply use CFPropertyListCreateDeepCopy. Here is example (with ARC):

        NSDictionary *newDictionary =
     (__bridge NSDictionary *)(CFPropertyListCreateDeepCopy(kCFAllocatorDefault,
             (__bridge CFPropertyListRef)(originalDictionary),
              kCFPropertyListImmutable));
    

    my originalDictionary in this example is an NSDictionary

    CFPropertyListRef can simply be cast to NSDictionary as long as the top level entity of the CFPropertyListRef is a CFDictionary.

提交回复
热议问题