[英]Convert NSArray (Swift) to vector (C++)
I'm trying to pass an integer NSArray from Swift to C++ (vector), and I'm using Objective-C as a bridge between them, where the conversion will take place. 我在下面做了一个简单的例子,其中 Swift 中的一个数组被传递给 Objective-C,它被转换为一个向量来传递给 C++,它执行一些计算并返回结果。
样品.swift
let myArray: Array<Int> = [10, 24, 13, 34]
HelloWorldWrapper().returnArrayAsString(myArray)
样品.mm
@implementation HelloWorldWrapper
- (NSString *) returnArrayAsString:(NSArray *) myArray {
vector<int> myVector;
for (int j = 0; j< 4; ++j) {
int myInt = myArray[j]; // error: Cannot initialize a variable of type 'int' with an rvalue of type 'id'
myVector.push_back(myInt)
}
}
还有很多其他的事情,我尝试过也会导致错误。 最终我的目标是将 Array[Int] 转换为要在 C++ 中使用的向量。
我目前在 windows 机器上,因此无法测试目标 C++ 代码。 但是这样的事情怎么样? 我相信Array<Int>
被桥接为NSArray<NSNumber *>
- (NSString *) returnArrayAsString:(NSArray<NSNumber *> *) myArray {
vector<int> myVector;
for (int j = 0; j< 4; ++j) {
int myInt = [myArray[j] intValue];
myVector.push_back(myInt)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.