定义一个结构并在Objective-C(或Objective-C ++)中的NSArray中使用它

时间:2015-11-05 11:35:39

标签: objective-c objective-c++

我正在编写一个包装器来从Swift调用一些C ++。 I understand要调用C ++ std::vector我应该使用NSArray,但是下面的代码无法使用错误编译最后两个函数参数

  

类型参数'PolylineElement'既不是Objective-C对象也不是块类型

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

如何声明结构的NSArray

如果按照trojanfoe在评论中的建议我将PolylineElement变成一个类,那么

@interface PolylineElement : NSObject
{
    int x1;
    int y1;
    int x2;
    int y2;
}
@end

然后我收到错误

  

类型参数'PolylineElement'必须是指针(需要'*')

但是如果我修复了函数调用以便使用指针

@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

我没有遇到编译器问题,但是数组的每个元素现在都是一个指针,而不是C ++所期望的,即有一个额外的间接级别。

0 个答案:

没有答案