How to return a C-array from method in Objective-C?(如何从 Objective-C 中的方法返回 C 数组?)
问题描述
我有一个返回变量的函数,我想知道如何返回一个数组,问题是它不是一个 NSArray,它只是一个像这样的普通 C 数组...
I have a function that returns a variable and I want to know how to return an array the issue is it isn't an NSArray it is just an average C array like this...
-(b2Fixture*) addFixturesToBody:(b2Body*)body forShapeName:(NSString*)shape
{
BodyDef *so = [shapeObjects objectForKey:shape];
assert(so);
FixtureDef *fix = so->fixtures;
int count = -1;
b2Fixture *Fixi[4];
while(fix)
{
count++;
NSLog(@"count = %d",count);
Fixi[count]= body->CreateFixture(&fix->fixture);
if (Fixi[count]!=0) {
NSLog(@"Fixi %d is not 0",count);
}
if (body->CreateFixture(&fix->fixture)!=0) {
NSLog(@"body %d is not 0",count);
}
fix = fix->next;
}
return *Fixi;
}
如果你看到一些你不知道的变量类型,那是因为我正在使用 cocos2d 框架制作游戏,但我返回的是 b2Fixture 的变量...此代码编译但只保存第一个块的值数组fixi [0]"不是我想要传递的整个数组
If you see some variable types you don't know it's because I'm using cocos2d framework to make a game but I'm returning a variable of b2Fixture... This code compiles however only saves the value of the first block of the array "fixi[0]" not the whole array like I want to pass
任何帮助 :) 谢谢你
anyhelp :) thankyou
推荐答案
不能返回本地数组.您需要进行某种动态分配或使用诸如将数组放入结构中的技巧.
You can't return a local array. You'll need to do some kind of dynamic allocation or pull a trick like having the array inside a structure.
这里有一个链接到一篇深入的文章应该能帮到你.
Here is a link to an in-depth article that should help you out.
这篇关于如何从 Objective-C 中的方法返回 C 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 Objective-C 中的方法返回 C 数组?
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
