gpt4 book ai didi

ios - 为什么在打印核心位置返回到控制台的位置时出现无限循环?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:10:17 26 4
gpt4 key购买 nike

阅读 big nerd ranch ios 编程书籍并时常遇到问题,因为这本书是基于 ios6 的编码,而我使用的是最新版本的 XCode/ios7。

无论如何,目前正在使用 Core Location 框架并将其添加到我的目标列表中。目前正在尝试获取模拟位置为英国伦敦的设备的位置。这是我的代码,它有时可以工作,但如果无法获取位置,有时会抛出错误。

.h接口(interface)文件:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface WhereamiViewController : UIViewController
{
CLLocationManager *locationManager;

}

@end

.m实现文件:

#import "WhereamiViewController.h"

@interface WhereamiViewController ()

@end

@implementation WhereamiViewController

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];

if (self){
//create location manager object
locationManager = [[CLLocationManager alloc] init];

//there will be a warning from this line of code
[locationManager setDelegate:self];

//and we want it to be as accurate as possible
//regardless of how much time/power it takes
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

//tell our manager to start looking for its location immediately
[locationManager startUpdatingLocation];
}

return self;
}

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@", newLocation);
}

- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Could not find location: %@", error);
}


@end

控制台日志:

2013-10-13 19:21:43.546 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:43 PM British Summer Time
2013-10-13 19:21:44.545 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:44 PM British Summer Time
2013-10-13 19:21:45.546 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:45 PM British Summer Time
2013-10-13 19:21:46.546 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:46 PM British Summer Time
2013-10-13 19:21:47.547 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:47 PM British Summer Time
2013-10-13 19:21:48.548 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:48 PM British Summer Time
2013-10-13 19:21:48.925 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:48 PM British Summer Time
2013-10-13 19:21:49.926 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @

如您所见,这是一个无限循环。处理这种情况的最佳方法是什么?我还在学习,不想让事情复杂化。我想保持阅读本书的步伐。我明白我遇到的这些小问题可能对我的学习有益。试图了解做旧事情的新方法让我意识到有时我必须回到旧方法并在以新方法做之前完全理解它。

帮助将不胜感激问候。

最佳答案

这不是无限循环。 locationManager:didUpdateToLocation:fromLocation 是位置管理器定期调用的委托(delegate)方法,用于使用手机的新位置更新您的 WhereamiViewController。

行内

[locationManager setDelegate:self];

您已经告诉 locationManager 在您的 View Controller 上调用 CLLocationManagerDelegate 协议(protocol)的委托(delegate)方法。可以通过替换

来避免由该行引起的警告
@interface WhereamiViewController : UIViewController

在你的 .h 文件中

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate>

它告诉编译器 WhereamIViewController 遵守 CLLocationManagerDelegate 协议(protocol)。您应该注意委托(delegate)方法 locationManager:didUpdateToLocation:fromLocation 在 iOS6 中已弃用,您应该改用 locationManager:didUpdateLocations。可以找到 CLLocationManagerDelegate 文档 here .

你可以使用

[locationManager stopUpdatingLocation];

停止通过委托(delegate)方法接收位置更新或

locationManager.pausesLocationUpdatesAutomatically = YES;

允许位置管理器在位置数据不太可能更改时暂停更新。

有关这种情况的更多信息,了解它是 Objective-C 编程的基础,您应该阅读 Objective-C Protocols

关于ios - 为什么在打印核心位置返回到控制台的位置时出现无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19348782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com