将浮点值分配给 NSMutableArray
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
向数组添加数字
NSArray (NSMutableArray)
不允许您向其中添加原始类型。这是因为 NSArray 是一组简单的指向您添加到其中的对象的指针。这意味着如果您想向数组添加一个数字,您首先必须将其包装在NSNumber
中。数字类型转换
NSNumber 允许您轻松转换数字类型,例如从 int 转换为 float
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 aNSNumber
.Number type conversion
NSNumber allows you to easily convert the type of a number e.g. from an int to a float
如果数组为空(nil)那么也许你还没有初始化它?
不要忘记,如果您分配了它,则需要在完成后释放它。
If the array is null (nil) then maybe you haven't initialised it?
Don't forget if you alloc it, you need to release it when finished with it.
for 循环的每次迭代都会创建一个新数组。你想要这样做:
You are creating a new array every iteration of the for loop. You want to do this:
使用 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