将对象添加到 NSMutableArray 或将 Float 转换为 NSNumber
Adding object to NSMutableArray or Converting Float to NSNumber
显然在向 NSMutableArray 添加元素或将浮点数转换为 NSNumber 时遇到困难。
这是一个有效的 while
循环,它使用简单的 C 数组而不是 NSMutableArray。它是用 printf's
测试的:
#define MAXSIZE 50
float xCoordinate [MAXSIZE] = {0.0};
float yCoordinate [MAXSIZE] = {0.0};
float x;
float y;
// ... some code creating handle from a .txt file
while ((fscanf(handle, " (%f;%f)", &x, &y)) == 2)
{
printf("%f\n",x);
printf("%f\n",y);
xCoordinate[n] = x;
yCoordinate[n] = y;
printf("Here is value from the array %f\n", xCoordinate[n]);
printf("Here is value from the array %f\n", yCoordinate[n] );
n++;
if ( n >= MAXSIZE)
{
break;
}
}
我想切换到 NSMutable 数组的原因是因为我想计算 xCoordinate 或 yCoordinate 数组中元素的数量。
我认为将函数 count
发送到数组更容易。
这里有一些循环对我不起作用,并且不将 scanf 的值分配给 x 和 y。
使用while
循环:
NSMutableArray *xCoordinate;
NSMutableArray *yCoordinate;
float x;
float y;
int n =0;
while ( ( fscanf(handle, " (%f;%f)", &x, &y) ) == 2)
{
// these 2 lines don't work as suppose to. What's wrong?
[xCoordinate addObject:[NSNumber numberWithFloat:x]];
[yCoordinate addObject:[NSNumber numberWithFloat:y]];
printf("asdf %f\n", [[xCoordinate objectAtIndex:n] floatValue]);
printf("asdf %f\n", [[yCoordinate objectAtIndex:n] floatValue]);
n++;
if ( n >= MAXSIZE)
{
break;
}
}
使用 for
循环:
NSMutableArray *xCoordinate;
NSMutableArray *yCoordinate;
float x;
float y;
for (n=0; ((fscanf(handle, " (%f;%f)", &x, &y)) == 2); n++)
{
// these 2 lines don't work as suppose to. What's wrong?
[xCoordinate addObject:[NSNumber numberWithFloat:x]];
[yCoordinate addObject:[NSNumber numberWithFloat:y]];
printf("asdf %f\n", [[xCoordinate objectAtIndex:n] floatValue]);
printf("asdf %f\n", [[yCoordinate objectAtIndex:n] floatValue]);
if ( n >= MAXSIZE)
{
break;
}
}
您永远不会创建数组实例,所以您总是试图将项目添加到空,这在 Objective-C 中是一个静默的空操作。将第一行更改为:
NSMutableArray *xCoordinate = [NSMutableArray array];
NSMutableArray *yCoordinate = [NSMutableArray array];
显然在向 NSMutableArray 添加元素或将浮点数转换为 NSNumber 时遇到困难。
这是一个有效的 while
循环,它使用简单的 C 数组而不是 NSMutableArray。它是用 printf's
测试的:
#define MAXSIZE 50
float xCoordinate [MAXSIZE] = {0.0};
float yCoordinate [MAXSIZE] = {0.0};
float x;
float y;
// ... some code creating handle from a .txt file
while ((fscanf(handle, " (%f;%f)", &x, &y)) == 2)
{
printf("%f\n",x);
printf("%f\n",y);
xCoordinate[n] = x;
yCoordinate[n] = y;
printf("Here is value from the array %f\n", xCoordinate[n]);
printf("Here is value from the array %f\n", yCoordinate[n] );
n++;
if ( n >= MAXSIZE)
{
break;
}
}
我想切换到 NSMutable 数组的原因是因为我想计算 xCoordinate 或 yCoordinate 数组中元素的数量。
我认为将函数 count
发送到数组更容易。
这里有一些循环对我不起作用,并且不将 scanf 的值分配给 x 和 y。
使用while
循环:
NSMutableArray *xCoordinate;
NSMutableArray *yCoordinate;
float x;
float y;
int n =0;
while ( ( fscanf(handle, " (%f;%f)", &x, &y) ) == 2)
{
// these 2 lines don't work as suppose to. What's wrong?
[xCoordinate addObject:[NSNumber numberWithFloat:x]];
[yCoordinate addObject:[NSNumber numberWithFloat:y]];
printf("asdf %f\n", [[xCoordinate objectAtIndex:n] floatValue]);
printf("asdf %f\n", [[yCoordinate objectAtIndex:n] floatValue]);
n++;
if ( n >= MAXSIZE)
{
break;
}
}
使用 for
循环:
NSMutableArray *xCoordinate;
NSMutableArray *yCoordinate;
float x;
float y;
for (n=0; ((fscanf(handle, " (%f;%f)", &x, &y)) == 2); n++)
{
// these 2 lines don't work as suppose to. What's wrong?
[xCoordinate addObject:[NSNumber numberWithFloat:x]];
[yCoordinate addObject:[NSNumber numberWithFloat:y]];
printf("asdf %f\n", [[xCoordinate objectAtIndex:n] floatValue]);
printf("asdf %f\n", [[yCoordinate objectAtIndex:n] floatValue]);
if ( n >= MAXSIZE)
{
break;
}
}
您永远不会创建数组实例,所以您总是试图将项目添加到空,这在 Objective-C 中是一个静默的空操作。将第一行更改为:
NSMutableArray *xCoordinate = [NSMutableArray array];
NSMutableArray *yCoordinate = [NSMutableArray array];