I'm writing a wrapper to call some C++ from Swift. I understand that to call a C++ std::vector
I should use NSArray
, but the following code fails to compile on the last two function arguments with the error
Type argument 'PolylineElement' is neither an Objective-C object nor a block type
struct PolylineElement
{
int x1;
int y1;
int x2;
int y2;
};
@interface CPPWrapper : NSObject
- (void) grabberInitializeAndProcessFromPixels: (unsigned char*) pbInPixels
andInStride: (int) inStride
andOutPixels: (unsigned char*) pbOutPixels
andOutStride: (int) outStride
andWidth: (int) width
andHeight: (int) height
andMarqueeTopLeft: (Point) mqTopLeft
andMarqueeTopRight: (Size) mqSize
// Next two lines fail
andForegroundMarks: (NSArray<PolylineElement> *) pForegroundMarks
andBackgroundMarks: (NSArray<PolylineElement> *) pBackgroundMarks
andGrabberState: (void *) pGrabberState;
@end
How do I declare an NSArray
of a struct?
If, following trojanfoe's advice in the comments I make PolylineElement
into a class thus
@interface PolylineElement : NSObject
{
int x1;
int y1;
int x2;
int y2;
}
@end
then I get the error
Type argument 'PolylineElement' must be a pointer (requires a '*')
But if I fix the function call to use pointers thus
@interface CPPWrapper : NSObject
- (void) grabberInitializeAndProcessFromPixels: (unsigned char*) pbInPixels
andInStride: (int) inStride
andOutPixels: (unsigned char*) pbOutPixels
andOutStride: (int) outStride
andWidth: (int) width
andHeight: (int) height
andMarqueeTopLeft: (Point) mqTopLeft
andMarqueeTopRight: (Size) mqSize
andForegroundMarks: (NSArray<PolylineElement *> *) pForegroundMarks
andBackgroundMarks: (NSArray<PolylineElement *> *) pBackgroundMarks
andGrabberState: (void *) pGrabberState;
@end
I do not get compiler issues but each element of the array is now a pointer, not what the C++ is expecting, i.e. there is an extra level of indirection.