将浮点值分配给 NSMutableArray

发布于 2024-12-10 18:17:46 字数 958 浏览 0 评论 0原文

我想在 for 循环中将浮点变量分配给 nsmutable 数组。我是像下面这样做的。但 nsmutable 数组似乎为空。我该如何解决这个问题? (距离是 float,enyakinarray 是 NSMutablearray。)

for (int i=0; i<[ws3.CustomerID count]; i++) {

    //radian hesaplaması
    float total = [first floatValue];
    float theta = total * M_PI/180;
    float total2 = [second floatValue];
    float theta2 = total2 * M_PI/180;
    float total3 = [[ws3.Latitude objectAtIndex: i]  floatValue];
    float theta3 = total3 * M_PI/180;
    float total4 = [[ws3.Longitude objectAtIndex: i] floatValue];
    float theta4 = total4 * M_PI/180;


     distance = 6371 * acos(cos(theta) * cos(theta3)
                           * cos(theta4 - theta2)
                           + sin(theta) * sin( theta3)) ;

    NSLog(@"xxxxx %f",distance);

    num = [NSNumber numberWithFloat:distance];
    enyakinarray = [[NSMutableArray alloc] init];
    [enyakinarray  addObject:num];
    NSLog(@"asasasas %@",enyakinarray);

}

I want to assign float variables to nsmutable array in a for loop. I did it like below. But the nsmutable array is seems null. How can I solve this? (distance is float and enyakinarray is NSMutablearray.)

for (int i=0; i<[ws3.CustomerID count]; i++) {

    //radian hesaplaması
    float total = [first floatValue];
    float theta = total * M_PI/180;
    float total2 = [second floatValue];
    float theta2 = total2 * M_PI/180;
    float total3 = [[ws3.Latitude objectAtIndex: i]  floatValue];
    float theta3 = total3 * M_PI/180;
    float total4 = [[ws3.Longitude objectAtIndex: i] floatValue];
    float theta4 = total4 * M_PI/180;


     distance = 6371 * acos(cos(theta) * cos(theta3)
                           * cos(theta4 - theta2)
                           + sin(theta) * sin( theta3)) ;

    NSLog(@"xxxxx %f",distance);

    num = [NSNumber numberWithFloat:distance];
    enyakinarray = [[NSMutableArray alloc] init];
    [enyakinarray  addObject:num];
    NSLog(@"asasasas %@",enyakinarray);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星軌x 2024-12-17 18:17:46

向数组添加数字
NSArray (NSMutableArray) 不允许您向其中添加原始类型。这是因为 NSArray 是一组简单的指向您添加到其中的对象的指针。这意味着如果您想向数组添加一个数字,您首先必须将其包装在 NSNumber 中。

NSArray *numbers=[NSArray arrayWithObject:[NSNumber numberWithInteger:2]];

数字类型转换

NSNumber 允许您轻松转换数字类型,例如从 int 转换为 float

NSNumber *number=[NSNumber numberWithInteger:2];
float floatValue = [number floatValue];

Adding a number to an array
NSArray (NSMutableArray) doesnt allow you to add primitive types to it. This is because an NSArray is simple a set of pointers to the Objects you add to it. This means if you want to add a number to the array, you first have to wrap it in a NSNumber.

NSArray *numbers=[NSArray arrayWithObject:[NSNumber numberWithInteger:2]];

Number type conversion

NSNumber allows you to easily convert the type of a number e.g. from an int to a float

NSNumber *number=[NSNumber numberWithInteger:2];
float floatValue = [number floatValue];
生来就爱笑 2024-12-17 18:17:46

如果数组为空(nil)那么也许你还没有初始化它?

enyakinarray = [[NSMutableArray alloc] init];

不要忘记,如果您分配了它,则需要在完成后释放它。

[enyakinarray release];

If the array is null (nil) then maybe you haven't initialised it?

enyakinarray = [[NSMutableArray alloc] init];

Don't forget if you alloc it, you need to release it when finished with it.

[enyakinarray release];
顾北清歌寒 2024-12-17 18:17:46

for 循环的每次迭代都会创建一个新数组。你想要这样做:

NSMutableArray *enyakinarray = [[NSMutableArray alloc] init];
for (int i=0; i<[ws3.CustomerID count]; i++) {

    //radian hesaplaması
    float total = [first floatValue];
    float theta = total * M_PI/180;
    float total2 = [second floatValue];
    float theta2 = total2 * M_PI/180;
    float total3 = [[ws3.Latitude objectAtIndex: i]  floatValue];
    float theta3 = total3 * M_PI/180;
    float total4 = [[ws3.Longitude objectAtIndex: i] floatValue];
    float theta4 = total4 * M_PI/180;


     distance = 6371 * acos(cos(theta) * cos(theta3)
                           * cos(theta4 - theta2)
                           + sin(theta) * sin( theta3)) ;

    NSLog(@"xxxxx %f",distance);

    num = [NSNumber numberWithFloat:distance];
    [enyakinarray  addObject:num];

}
NSLog(@"asasasas %@",enyakinarray);

You are creating a new array every iteration of the for loop. You want to do this:

NSMutableArray *enyakinarray = [[NSMutableArray alloc] init];
for (int i=0; i<[ws3.CustomerID count]; i++) {

    //radian hesaplaması
    float total = [first floatValue];
    float theta = total * M_PI/180;
    float total2 = [second floatValue];
    float theta2 = total2 * M_PI/180;
    float total3 = [[ws3.Latitude objectAtIndex: i]  floatValue];
    float theta3 = total3 * M_PI/180;
    float total4 = [[ws3.Longitude objectAtIndex: i] floatValue];
    float theta4 = total4 * M_PI/180;


     distance = 6371 * acos(cos(theta) * cos(theta3)
                           * cos(theta4 - theta2)
                           + sin(theta) * sin( theta3)) ;

    NSLog(@"xxxxx %f",distance);

    num = [NSNumber numberWithFloat:distance];
    [enyakinarray  addObject:num];

}
NSLog(@"asasasas %@",enyakinarray);
飘落散花 2024-12-17 18:17:46

使用 CGFloat 代替,我知道这有效。另外,由于我下面的评论,我想起了一件事。 NSNumber 也可以工作,这里是类参考 http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnumber_Class/Reference/Reference.html

Use CGFloat instead, I know that works. Also, I was reminded of something because of the comment below mine. NSNumber will work too, here is the class reference http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnumber_Class/Reference/Reference.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文