iPhone 比较课程价值的问题

发布于 2024-08-28 08:48:03 字数 529 浏览 8 评论 0原文

我正在编写一些代码来获取一些值,包括课程

    -(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";}

等,但我总是检索第一个值“N”。

我的错误在哪里?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦亿 2024-09-04 08:48:03

您可能需要将逻辑 OR 更改为逻辑与(将 || 更改为 &&),这将确保该值介于 0 和 90 之间,或者 90 和180.

由于逻辑或,逻辑对我来说似乎也有点缺陷,也许我不理解你所做的假设 - 但如果该值是,比如说,200,它将通过第一个 if 因为 200 大于 0。然后它也会通过第二个 if,因为 200 大于 90。由于逻辑 OR,它们会通过。只有其中一个语句 (>= 0 OR < 90) 必须为真才能通过。

这可以通过使用逻辑 AND 来解决。

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.

调妓 2024-09-04 08:48:03

您不需要通过 NSString 来检查课程,但错误的根本原因是课程是双精度的,您应该在字符串格式中使用 %f

较短:

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

但实际上我认为你的算法是错误的。如果路线为 0<=course<45 或 315<=course<360 ,则您将前往北方。

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 .

记忆消瘦 2024-09-04 08:48:03

@yonel

谢谢,课程是学位:-(
但我不明白你的算法,阅读苹果文档我发现了这一点:

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

对我来说这意味着

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

我们说的是同一件事吗? :D

@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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文