objC - empty array of types
In C, C++, C# you can have arrays of a certain type like int[]. In objC a NSArray can have a different type for every element. If you want to have an NSArray of type x, you now can just treat the type of the element at index 0 as type of the array, meaning if [anArray objectAtIndex:0] is a NSString, you treat all objects as a NSString and throw an error, if any element is of a different type than the element at index 0. It is ugly, but you can simulate typed arrays that way. Now, in C# it is even possible to have an empty array of type x with 0 elements. However an empty NSArray of course has no element, not even at index 0, so there is no typeinfo in there.
Now if an objC app gets an array from a C# app or vice versa and the 开发者_如何学GoC# app in some situations sends or expects an empty array, the receiving side has to know the type of, how can get/set this typeinfo in objC?
Of course I could use C-arrays instead of NSArrays, but if you want to store a c-array in a NSDictionary, you have to put it into a NSValue first and a NSValue does not offer a possibility to store the size of a dynamically allocated c-array.
Is there any better option than to implement a new class "CArrayValue" for storing C_Arrays with typeinfo and size in a NSDictionary?
Make a wrapper for NSArray, one that contains a type key as instance variable and an NSArray. In that way you should be able to easily control the type of the array. Also what you could do to prevent adding types that are not allowed, is writing some methods that would remove / add items t o the array / initialize it.
精彩评论