【问题标题】:convert c++ vector to Objective-c将 c++ 向量转换为 Objective-c
【发布时间】:2017-10-10 20:29:23
【问题描述】:

我现在有

vector<vector<cv::Point>> example;

并且希望能够将它传递给一个 swift 函数,所以我认为我应该从这样开始(或者有更简单的方法吗?):

NSArray[NSArray[Int,Int]]

cv::Point 是两个 int 属性的对象。

我试过了

NSMutableArray *example1[example.size()];
for(i = 0;i< example.size();i++){
        if(example[i].size() > 5){ //irrelevant
            NSMutableArray *second[example[i].size()];
            for(int p = 0;p<example[i].size();p++){
                NSInteger p1 = example[i][p].x;
                NSInteger p2 = example[i][p].y;
                NSInteger point[2] = {p1,p2};
                [second addObject: point];
            }
          [example addObject: second];
       }
 }

我已经花费了很长时间,并且不断收到不同的数据类型错误等。你会怎么做,或者更正这个?

谢谢

【问题讨论】:

标签: c++ ios objective-c arrays swift


【解决方案1】:

您可以使用NSValue 将一个简单的结构存储到NSMutableArray

NSMutableArray *example1 = [NSMutableArray arrayWithCapacity:example.size()];
for(int i = 0;i< example.size();i++){
    if(example[i].size() > 5){ //irrelevant
        NSMutableArray *second = [NSMutableArray arrayWithCapacity:example[i].size()];
        for(int p = 0;p<example[i].size();p++){
            NSValue *point = [[NSValue alloc] initWithBytes:&example[i][p] objCType:@encode(cv::Point)];
            [second addObject:point];
        }
        [example1 addObject:second];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 2012-11-15
    • 1970-01-01
    • 2017-10-24
    • 2011-09-17
    • 2012-12-16
    • 1970-01-01
    相关资源
    最近更新 更多