0

i have a loop in Objective C and i compare two index of the same array but the comparison doesnt work ,there is no compile error. Variable iMinor is always zero and arrayDistancias is not empty:

NSMutableArray *arrayDistancias = [NSMutableArray array];


NSNumber *lat1 = [NSNumber numberWithFloat:39.0025];
NSNumber *lat2 = [NSNumber numberWithFloat:40.2710];
NSNumber *lat3 = [NSNumber numberWithFloat:38.5316];
NSNumber *lat4 = [NSNumber numberWithFloat:27.4529];
NSNumber *lng1 = [NSNumber numberWithFloat:-1.5139];
NSNumber *lng2 = [NSNumber numberWithFloat:-3.4327];
NSNumber *lng3 = [NSNumber numberWithFloat:-7.0029];
NSNumber *lng4 = [NSNumber numberWithFloat:-15.3432];


//for (NSInteger i = 0; i < 40; i++)
[arrayDistancias addObject:[NSNumber numberWithInteger:[self calcularDistancia:latitudaqui :lat1 :longitudaqui :lng1]]];
[arrayDistancias addObject:[NSNumber numberWithInteger:[self calcularDistancia:latitudaqui :lat2 :longitudaqui :lng2]]];
[arrayDistancias addObject:[NSNumber numberWithInteger:[self calcularDistancia:latitudaqui :lat3 :longitudaqui :lng3]]];
[arrayDistancias addObject:[NSNumber numberWithInteger:[self calcularDistancia:latitudaqui :lat4 :longitudaqui :lng4]]];


int iMinor = 0;
for (int i = 0; i < [arrayDistancias count]; i++) {

        if([arrayDistancias objectAtIndex: i] < [arrayDistancias objectAtIndex: iMinor]){
            iMinor = i;
          }              


     }

thanks in advance

1 Answer 1

2

[arrayDistances objectAtIndex: i] retuns the NSNumber object, not the integer value. In order to get the integer value use intValue method on the returned object. So, you should compare it like this.

if ([[arrayDistances objectAtIndex: i] intValue] < 
   [[arrayDistances objectAtIndex: iMinor] intValue]) {
Sign up to request clarification or add additional context in comments.

1 Comment

Just a note in the example float are used, so you could use floatValue to get the float value. Or just use compare method of NSNumber. if ([[arrayDistances objectAtIndex: i] compare:[arrayDistances objectAtIndex: iMinor]] == NSOrderedAscending)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.