xcode 将浮点值分配给 NSMutableArray
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7838101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assign float values to NSMutableArray
提问by Hacer sengul Akac
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 循环中将浮点变量分配给 nmutable 数组。我像下面那样做。但是 nsmutable 数组似乎为空。我该如何解决这个问题?(距离是浮点数,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);
}
回答by Gabriel
Adding a number to an arrayNSArray (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 (NSMutableArray)
不允许您向其添加基本类型。这是因为 NSArray 是一组简单的指向添加到它的对象的指针。这意味着如果你想向数组中添加一个数字,你首先必须将它包装在一个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 允许您轻松转换数字的类型,例如从 int 转换为 float
NSNumber *number=[NSNumber numberWithInteger:2];
float floatValue = [number floatValue];
回答by NJones
You are creating a new array every iteration of the for loop. You want to do this:
您在 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);
回答by Matt Fellows
If the array is null (nil) then maybe you haven't initialised it?
如果数组为 null (nil) 那么也许你还没有初始化它?
enyakinarray = [[NSMutableArray alloc] init];
Don't forget if you alloc it, you need to release it when finished with it.
不要忘记如果你分配它,你需要在完成它时释放它。
[enyakinarray release];
回答by Gabriel
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
使用 CGFloat 代替,我知道这可行。另外,由于我下面的评论,我想起了一些事情。NSNumber 也可以工作,这里是类参考http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnumber_Class/Reference/Reference.html