将浮点值赋给NSMutableArray

Hac*_*kac 2 floating-point xcode objective-c nsmutablearray ios

我想在for循环中将浮点变量赋给nsmutable数组.我是这样做的.但是,nsmutable数组似乎为null.我怎么解决这个问题?(距离是浮点数,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);

}
Run Code Online (Sandbox Code Playgroud)

Gab*_*iel 5

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

NSArray *numbers=[NSArray arrayWithObject:[NSNumber numberWithInteger:2]];
Run Code Online (Sandbox Code Playgroud)

数字类型转换

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

NSNumber *number=[NSNumber numberWithInteger:2];
float floatValue = [number floatValue];
Run Code Online (Sandbox Code Playgroud)