Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign float values to NSMutableArray

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

}
like image 758
Hacer sengul Akac Avatar asked Dec 06 '25 10:12

Hacer sengul Akac


2 Answers

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];
like image 196
Gabriel Avatar answered Dec 07 '25 22:12

Gabriel


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];
like image 43
Matt Fellows Avatar answered Dec 07 '25 23:12

Matt Fellows



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!