如何在 Objective-C 数组中存储像下面这样的 Objective-C++ 短整型,并稍后将其转换回 Objective-C++?我尝试了以下操作但没有成功。任何帮助都会很棒!
short int *tmpbuffer = ( short int*)malloc(sizeof(short int)*length*inData);
int count = 0;
for (count = 0; count < length*inData; count++)
{
tmpbuffer[count] = (short int) (inbuffer[count] * 0x7fff);
}
size = fwrite(tmpbuffer, 1, sizeof(short int)*length*inData,fp);
[fwriteSections addObject:[NSNumber numberWithShort:tmpbuffer]];
[fwriteSections addObject:[NSNumber numberWithShort:sizeof(short int)*length*inchannels]];
[fwriteRows addObject:fwriteSections];
最佳答案
对于简单的字节缓冲区,不需要在 Objective C++ 和 Objective C 之间进行任何转换。您可以在 Objective C++ 和 Objective C 类之间传递一个 short int
指针。
如果您的意思是,如何将 short int
字节缓冲区转换为 NSArray
,那么您就在正确的轨道上,只需执行以下操作:
short int *buffer = malloc(size * sizeof(short int));
NSMutableArray *shortArray = [NSMutableArray arrayWithCapacity:size];
for (NSUInteger i = 0; i < size; i++) {
[shortArray addObject:[NSNumber numberWithShort:buffer[i]]];
}
不过我不推荐这种方法,它效率不高,你最好坚持使用 C 风格的缓冲区。
关于iphone - 将值从 Objective-C++ 存储/检索到 Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9407024/