ios - Obj-C - 如何将 id 转换为 GLfloat[]

标签 ios objective-c arrays nsmutablearray

我有一个 NSMutableArray GLfloat 的简单数组。 这就是我在 Swift 中构建它的方式:

var arrays = NSMutableArray()
for(i=0; i<arraysCount; i++) {
    var array:[GLfloat] = []

    // fill the array

    arrays.appand(array)
}

但是我如何在 Objective-C 中通过索引获取我的数组? [arrays objectAtIndex:index] 给了我一个 ID,但是如何将它转换成 GLfloat 数组?

我需要使用 GLfloat 数组,因为必须绘制此数组并且我将它们发送到 glBufferData()

最佳答案

在 objective-c 中,您不能将简单的 C 类型添加到 NSMutableArray 中,NSMutableArray 只能包含 objective-c 对象。因此,调用 [arrays objectAtIndex:index]; 时获得的数组很可能是 NSNumberNSValue 的数组。

在 objective-c 中,您可以使用 C 样式数组,例如int myArr[5] = {1,2,3,4,5};NSArray 但你不能混合使用它们。因此 intNSArray 将是 NSArray *myArr = @[@1,@2,@3,@4,@5]其中 @1NSNumber 对象,其 intValue 为 1。

因此,要将您的数组转换为 float 数组,您可以执行以下操作:

float otherArr[(int)myArr.count];
for (int i = 0; i < myArr.count; i++) {
    NSNumber *num = myArr[i];
    otherArr[i] = myArr.floatValue;
}
// Voilia a C style array of floats

这是 Swift 在使用 objective-c 时可能会混淆的地方,因为我相信这一切都是隐式完成的。

关于ios - Obj-C - 如何将 id 转换为 GLfloat[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28200454/

相关文章:

objective-c - 如何强制在 Objective-C 中不使用匿名 block 的参数?

iOS 模拟器无法安装成功构建的应用程序 msg

c++ - 指针运算 : out of bound without dereferencing

ios - swift 中的通用 UITableViewController

ios - 主控/详细信息 - 如果对象被删除或主控中没有留下任何对象,则清除详细信息 (iOS)

iOS UIWebView loadRequest 耗时太长

javascript - 用对象和数组展平深度嵌套的数组

javascript - 将对象的属性值添加到对象数组

ipad - 将 ipadsDetailViewController 设置为 UIWebView

iphone - 带背景图像的 TableView