I've searched around but nothing useful for me founded.
What is the best way to write a swap method to exchange two primitive values in ObjectiveC
?
You know the primitive types have different sizes, so if we pass them through something like void *
, then how can we know about their size? (maybe one extra parameter for their size?)
in C#
, it could be something like this :
void Swap<T>(ref T a, ref T b)
{
T tmp = a;
a = b;
b = tmp;
}
An idea could be creating a temp memory block with the same size of input types, then copying them as memory blocks with a method like memcpy
or something.
But I prefer something more natural in ObjectiveC
, if there is a better one.