2012-02-23 22 views
3

如何在Objective-C数组中存储Objective-C++ short int并将其稍后转换回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]; 

回答

2

对于简单的字节缓冲区,不需要在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风格的缓冲区。