2

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

}

4 Answers 4

5

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

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];
4
  • Ok when I allocate the enyakinarray, it change every time. assign process is in the for loop. I want to do this: repeat the loop until another array count and distance calculated and add distance to enyakinarray. Oct 20, 2011 at 15:25
  • I want to keep every distances which calculated in for loop in enyakinarray. Oct 20, 2011 at 15:26
  • I'm happy to help, but need to see the code you've written so far. Edit your original question to put the updated code beneath your current code. - I will see what I can see with that. Oct 20, 2011 at 15:31
  • ok I edited my question. The code is above :) thnx for your help Oct 20, 2011 at 15:34
3

You are creating a new array every iteration of the for loop. 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);
0

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

4
  • Nothing is wrong with it, I rarely have ever used NSNumber. I always use CGFLoat
    – Gabriel
    Oct 20, 2011 at 15:23
  • I'll add another answer that solves your problem and tells you what is wrong
    – Gabriel
    Oct 20, 2011 at 17:06
  • You can't put CGFloats into an NSArray; they are primitives, and NSArray can only hold objects.
    – jscs
    Oct 20, 2011 at 18:15
  • Which is why i posted another answer
    – Gabriel
    Oct 20, 2011 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.