2012-02-23 64 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風格的緩衝區。