【问题标题】:Converting NSMutableArray to float array将 NSMutableArray 转换为浮点数组
【发布时间】:2013-02-11 21:26:24
【问题描述】:

我有一个 lat 和 lon 用 "," 分隔的字符串:

 NSArray* coord = [newString componentsSeparatedByString:@","];

 NSMutableArray *lat= [[NSMutableArray alloc] init];
 NSMutableArray *lon = [[NSMutableArray alloc] init];

 for(int i=0;i<[coord count]-1;i=i+2)

    {


          [lat addObject:[coord objectAtIndex:i]];
          [lon addObject:[coord objectAtIndex:i+1]];

    }

我想将它们与此功能一起使用:

 int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
    if ( ((verty[i]>testy) != (verty[j]>testy)) &&
        (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
        c = !c;
}
return c;

}

所以 NSMutableArray 不起作用。所以我创建了这个:

 //Seperating the string with coordinates by ","
    NSArray* coord = [newString componentsSeparatedByString:@","];

    float *lat = malloc(sizeof(float) * ([coord count]/2));
    float *lon = malloc(sizeof(float) * ([coord count]/2));


    for(int i=0;i<[coord count]-1;i=i+2)

    {

        lat[i]= [[coord objectAtIndex:i] floatValue];
        lon[i]= [[coord objectAtIndex:i+1] floatValue];


    }
   free(lat);
    free(lon);

这给了我一些错误,例如:(5629,0xacc3da28) malloc: * 对象 0x714c490 错误:已释放对象的校验和不正确 - 对象可能在被释放后被修改。

【问题讨论】:

    标签: arrays nsmutablearray parameter-passing


    【解决方案1】:

    我想你想使用数组中的值
    要获得这样的值,您可以使用 objectAtIndex,如:
    [arrayName objectAtIndex:index];

    如果你真的想给一个 NSMutableArray 作为参数,你应该这样做:
    functionName: (NSMutableArray *) arrayName

    【讨论】:

    • 其实我不想改变这个功能。用 [vert objectAtIndex:j] 替换 verity[j] 时出现错误
    • @user776720 你正在拿起一本好的 C 书并现在阅读它。 然后用 Objective-C 的书重复。 floats 不是对象。
    • lat[i]= [[coord objectAtIndex:i] floatValue];对象可以转换为浮点数
    • 你应该检查 j 是否小于数组的长度。
    • 您还应该检查数组是否正确初始化
    【解决方案2】:

    鉴于其他信息,我认为你想要这个。

    考虑到你已经建立了 NSMutableArray *lat 和 NSMutableArray *lon
    其中 lat 和 lon 的值不为 nil 或 null。

    for(int i=0;i<lat.count;i++)
    {
        pnpoly(lat.count, [lat objectAtIndex:i], [lon objectAtIndex:i], static, static);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 2019-07-05
      • 1970-01-01
      相关资源
      最近更新 更多