简体   繁体   English

将浮点值赋给NSMutableArray

[英]Assign float values to NSMutableArray

I want to assign float variables to nsmutable array in a for loop. 我想在for循环中将float变量赋给nsmutable数组。 I did it like below. 我是这样做的。 But the nsmutable array is seems null. 但是,nsmutable数组似乎为null。 How can I solve this? 我怎么解决这个问题? (distance is float and enyakinarray is NSMutablearray.) (距离是浮点数,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);

}

Adding a number to an array NSArray (NSMutableArray) doesnt allow you to add primitive types to it. 向数组添加数字 NSArray (NSMutableArray)不允许您向其添加基元类型。 This is because an NSArray is simple a set of pointers to the Objects you add to it. 这是因为NSArray是一组指向您添加到它的对象的简单指针。 This means if you want to add a number to the array, you first have to wrap it in a NSNumber . 这意味着如果要向数组添加数字,首先必须将其包装在NSNumber

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

Number type conversion 数字类型转换

NSNumber allows you to easily convert the type of a number eg from an int to a float NSNumber允许您轻松转换数字类型,例如从int转换为float

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

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];

You are creating a new array every iteration of the for loop. 您将在for循环的每次迭代中创建一个新数组。 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);

Use CGFloat instead, I know that works. 使用CGFloat代替,我知道它有效。 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 NSNumber也可以使用,这里是类参考http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnumber_Class/Reference/Reference.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM