CLLocation,获取新位置

发布于 2024-11-11 02:27:13 字数 605 浏览 3 评论 0原文

在《更多 iPhone 编程》一书中,作者是这样做的:

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
        if ([newLocation.timestamp timeIntervalSince1970] < [NSDate timeIntervalSinceReferenceDate] - 60)
        locationCoordinate = newLocation.coordinate;
return;
    ...

确保数据是在最后一刻获取的。两个问题:

1)if 语句是做什么的。看起来在左侧,您得到了此方法触发时与 1970 年日期之间的时间差(以秒为单位)。然后在右侧,您会看到 2001 年日期与现在的日期之间的秒差(负 60 秒)。所以对我来说, if 语句永远不会有效,因为左边的数据总是更长的秒数。还是我理解错了?

2)void函数中的return有什么作用?这在这里被认为是好的编码吗?谢谢。

In More iPhone Programming book, the author does:

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
        if ([newLocation.timestamp timeIntervalSince1970] < [NSDate timeIntervalSinceReferenceDate] - 60)
        locationCoordinate = newLocation.coordinate;
return;
    ...

To make sure the data was taken in the last minute. Two questions:

1) What is the if statement doing. It seems like on the left hand side, you are getting the time difference in seconds between when this method fires and the 1970 date. Then on the right hand side, you get the difference in seconds between the 2001 date and now minus 60 seconds. So to me, the if statement would never be valid since the data on the left is always going to be a greater amount of seconds. Or am I understanding it wrong?

2) What does return in a void function do? Is that considered good coding here? Thx.

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

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

发布评论

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

评论(2

灯角 2024-11-18 02:27:14

我不知道这里发生了什么,它的语句令人困惑,我使用了类似的东西

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
        NSDate* eventDate = newLocation.timestamp;
        NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
        if( abs(howRecent) > 1.0 )
               return;
        ////process your event here
}

将 return 放在 void 方法中是正确的,因为我不想在某些条件下执行接下来的语句..相同的代码可以这样写,因为

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
        NSDate* eventDate = newLocation.timestamp;
        NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
        if( abs(howRecent) < 1.0 ){
                 ///process your event
               }
}

它仅取决于您的需要。

I don't know what is going on here, its confusing statement, I have use something like this

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
        NSDate* eventDate = newLocation.timestamp;
        NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
        if( abs(howRecent) > 1.0 )
               return;
        ////process your event here
}

It is right to put return in void method, as I don't want to execute the statement next for some conditions..the same code can be written as

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
        NSDate* eventDate = newLocation.timestamp;
        NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
        if( abs(howRecent) < 1.0 ){
                 ///process your event
               }
}

it just depends upon your need.

梦魇绽荼蘼 2024-11-18 02:27:14

对于 if 块:

timeIntervalSinceReferenceDate 返回的值可能为负。参见说明。

如果接收者早于
参考日期,值为负数。

因此 if 条件可能为 false。

对于 return 语句。

如果您希望函数在到达某些 上的函数闭括号之前将控制权返回给调用函数条件。参见例如。

-(void) SomeFunction
{

   if(Condition1)
   { 
      return;
   }

}

for the if block :

the value return by timeIntervalSinceReferenceDate could be negative. see the description .

If the receiver is earlier than the
reference date, the value is negative.

So if condition could be false.

For the return statement.

if you want your function to return the control to the calling function before reaching the function closed bracket on some condition. See the eg.

-(void) SomeFunction
{

   if(Condition1)
   { 
      return;
   }

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