我正试图保持桌面行高度的浮动[]。我在这里找到了一个很好的课程:http://forums.macnn.com/t/224809/nsmutablearray-vs-a-plain-c-array-for-storing-floats。
问题在于,一旦调用了C函数(位于另一个文件中),我传入的浮点数就变为0.无论浮点值如何,每次都会发生这种情况。
C函数:
typedef struct
{
float *array;
int count;
} floatArray;
BOOL AddFloatToArray ( floatArray *farray, float newFloat )
{
if ( farray->count > 0 )
{
// The array is already allocated, just enlarge it by one
farray->array = realloc ( farray->array, ((farray->count + 1) * sizeof (float)) );
// If there was an error, return NO
if (farray->array == NULL)
return NO;
}
else
{
// Allocate new array with the capacity for one float
farray->array = (float *)malloc ( sizeof (float) );
// If there was an error, return NO
if (farray->array == NULL)
return NO;
}
printf("Adding float to array %f\n", newFloat);
farray->array[farray->count] = newFloat;
farray->count += 1;
printArrayContents(farray);
return YES;
}
int printArrayContents( floatArray* farray)
{
printf("Printing array contents\n");
for(int j=0; j < farray->count; j++)
printf("%f\n", farray->array[j]);
return 0;
}
来自:
NSDictionary* review = [self.reviews objectAtIndex:indexPath.row];
returnHeight = [ReviewCell cellHeightForReview:review];
NSLog(@"Adding height to array: %0.0f", returnHeight);
AddFloatToArray(heights, returnHeight);
以下是记录的内容:
2012-09-28 11:46:12.787 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 101
Adding float to array 0.000000
Printing array contents
0.000000
2012-09-28 11:46:12.788 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 138
Adding float to array 0.000000
Printing array contents
0.000000
0.000000
2012-09-28 11:46:12.788 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 122
Adding float to array 0.000000
Printing array contents
0.000000
0.000000
0.000000
2012-09-28 11:46:12.789 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 139
Adding float to array 0.000000
Printing array contents
0.000000
0.000000
0.000000
如何确保将正确的值实际插入float []?
答案 0 :(得分:2)
我没有声明.h中的函数#importing。所以编译并运行了所有内容,但这些类没有正确通信。你认为这会引发警告或错误。