views:

277

answers:

3

hi all,

i'm writing some code for getting some values including course

    -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation   *)newLocation fromLocation:(CLLocation *)oldLocation {
    //somecode
    NSString *dirString = [[NSString alloc] initWithFormat:@"%d", newLocation.course];
    int myInt = [dirString intValue];
    if ((myInt >= 0) || (myint  < 90)) {course.text =@ "N";}
    if ((myInt >= 90) || (myint  < 180)) {course.text =@ "E";}

and so on, but i always retrieve the first value, "N".

where's my mistake?

Thank's!

+2  A: 

You may want to change the logical OR to logical and (change || to &&) which will ensure that the value is between 0 and 90, or 90 and 180.

Because of the logical OR, the logic also seems a little flawed to me too, perhaps there is something I'm not understanding about the assumptions you've made - but if the value is, say, 200, it will pass the first if because 200 is greater than 0. It'll then also pass the second if because 200 is greater than 90. They pass because of the logical OR. Only one of the statements (>= 0 OR < 90) has to be true for it to pass.

This would be solved by using logical AND instead.

Jasarien
thank's jasarien,shure, i've understand this mistake
zebra
+1  A: 

You don't need to go via NSString to check the course, but the root cause of your bug is that the course is a double, you should use %f in the formating of your string.

shorter :

double theCourse = newLocation.course;
if ((theCourse >= 0) || (theCourse  < 90)) {course.text =@ "N";}
if ((theCourse >= 90) || (theCourse  < 180)) {course.text =@ "E";}

But actually I think your algo is wrong. You're going to the north if the course is 0<=course<45 or 315<=course<360 .

yonel
hi, i've try in your way and in another way: NSString *courseString = [[NSString alloc] initWithFormat:@"%3.0f", newLocation.course]; course.text = courseString;In simulator i've try to assign a value to courseString and works.On my device once get the value app crash and i've only mobile crash report (and i don't see nothing usefoul).Any ideas? It's happened every time i do this with a double.
zebra
if you want 3 digits after the dot in your string, it's "%.3f"
yonel
A: 

@yonel

thank's, course is in degree:-( but i've not understand your algo, reading on apple doc i've found this:

Thus, north is 0 degrees, east is 90degrees, south is180 
degrees, and so on. Course values may not be available on all 
devices.

for me this mean

course between 0 and 44= North;
course between 45 and 89= NE; 
course between 90 and 134= East; 
course between 135 and 179= SouthEast;
course between 180 and 234= South;
course between 235 and 269= SouthWest;
course between 270 and 314= West;
course between 315 and 360 = NorthWest; - 

Are we saying the same thing? :D

zebra