iPhone 比较课程价值的问题
我正在编写一些代码来获取一些值,包括课程
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要将逻辑 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 secondif
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.
您不需要通过 NSString 来检查课程,但错误的根本原因是课程是双精度的,您应该在字符串格式中使用
%f
。较短:
但实际上我认为你的算法是错误的。如果路线为 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 :
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
谢谢,课程是学位:-(
但我不明白你的算法,阅读苹果文档我发现了这一点:
对我来说这意味着
我们说的是同一件事吗? :D
@yonel
thank's, course is in degree:-(
but i've not understand your algo, reading on apple doc i've found this:
for me this mean
Are we saying the same thing? :D