Deepcopy of userData

Hi! Can I ask what’s the best way to deepcopy userData?

So far, I tried copy.deepcopy( userData ), as well as userData.__ deepcopy __(0), but both convert the dictionary to a list.
Iterating through userData.keys() and copy.deepcopying each of them separately results in “Cannot pickle Objective-C objects”.

So I’m curious if there’s a solution better than copying everything manually?

How deep is the structure?
I‘ll have a look if I can improve the deepcopy support. Until the. Look at the objectiveC API: [[NSMutableDictionary allocation] initWithDictionary:userData copyItems:YES]

1 Like

Thank you! In my case now, userData contains strings and dicts containing lists.

Upd, seemingly solved it like so:

def pythonize( objc_obj ):
	# unicode
	if isinstance( objc_obj, objc.pyobjc_unicode ):
		return str( objc_obj )
	# dict
	elif isinstance( objc_obj, NSMutableDictionary ):
		d = {}
		for key, value in objc_obj.items():
			k = pythonize( key )
			v = pythonize( value )
			d[ k ] = v
		return d
	# list
	elif isinstance( objc_obj, NSMutableArray ):
		l = []
		for item in objc_obj:
			l.append( pythonize(item) )
		return l
					
def deepcopyData( userData ):
	dataCopy = {}
	for key in userData.keys():
		dataCopy[ pythonize( key ) ] = pythonize( userData[ key ] )
	return dataCopy

That should work. But you can save a few calls to pythonize(). The pyobjc wrapper should take care of that.