I'm building an Objective-C wrapper for a nasty legacy C-library. Assume I have two Objective-C classes 开发者_如何学PythonA and B wrapping some related legacy structures. Now I would like to provide a function that converts a given object of class A into an object of class B. The problem is, that the conversion process destroys the internal state of the source object.
What is the best solution for implementing this conversion function regarding the standard Objective-C naming convention. I.e. it should be clear that the conversion function will do a release on the given source object.
Instead of asking about a name, perhaps you should ask for strategies on how to do the conversion process without destroying the state of the source object. I can't imagine any sort of C data that can't just by memcpy
'd to a new location. I'd fiddle around with doing that and seeing if duplicating the data before conversion will fix this issue for you.
releasing the source is not a good idea I believe. Why don't you make a copy of your source and release that (if that is neccesary). In that case you could call you method
MyObjectB * b = [MyObjectB bFromA:(MyObjectA *)a];
or
MyObjectB * b = [[MyObjectB alloc] initFromA:(MyObjectA *)a];
I guess ;)
There is no such naming convention. All the Objective-C methods/functions I've seen in standard APIs either copy their sources, leaving original source intact, or use original, but do not destroy it.
The naming convention that does exist, persists to whether the caller should release the new object.
精彩评论