deep mutable copy of a NSMutableDictionary

前端 未结 8 1914
醉话见心
醉话见心 2020-11-30 00:55

I am trying to create a deep-copy of a NSMutableDictionary and assign it to another NSMutableDictionary. The dictionary contains a bunch of arrays, each array containing nam

8条回答
  •  囚心锁ツ
    2020-11-30 01:06

    Assuming all elements of the array implement the NSCoding protocol, you can do deep copies via archiving because archiving will preserve the mutability of objects.

    Something like this:

    id DeepCopyViaArchiving(id anObject)
    {
        NSData* archivedData = [NSKeyedArchiver archivedDataWithRootObject:anObject];
        return [[NSKeyedUnarchiver unarchiveObjectWithData:archivedData] retain];
    }
    

    This isn't particularly efficient, though.

提交回复
热议问题