gpt4 book ai didi

ios - 在 iOS 中获取当前位置的静态方法

转载 作者:行者123 更新时间:2023-11-29 12:49:43 25 4
gpt4 key购买 nike

我是 iOS 编程的新手,正在编写一个练习应用程序来根据用户位置执行任务。

我想要的是拥有一个带有简单静态函数的 MyLocationManager 包装器类,类似于

+(CLLocation *) getCurrentLocation;

所以可以通用调用。然而,我发现通常获取位置的方法是实现 CLLocationManagerDelegate 接口(interface)以具有成功/失败处理程序,并且通常将代码放在 Controller 中。

我想知道是否有建议的方法来制作我提到的这种集中式实用程序类,并很好地包装检索位置代码,而无需它存在于许多不同的 Controller 中?

最佳答案

IMO,您想要存档的内容不是最佳做法。我认为在这种情况下最简洁的方法是使用 block 作为完成处理程序。看起来像这样

@implementation YourController

-(void)updateLocationAndDoSomething
{
[Utilities startUpdateLocationWithCompletionHandler: ^ (CLLocationCoordinate2D location, BOOL *successed){
//your custom code here
}];
}
@end

这里有一个关于 block http://www.appcoda.com/objective-c-blocks-tutorial/的简单易懂的教程如果你不知道该怎么做

第二种方式,使用delegation,你的 Controller 看起来像这样

@implementation YourController

-(void)gotLocationAndDoSomething:(CLLocationCoordinate2D) location
{
//your custom code here
}

-(void)failedToGetLocation
{

}
@end

如果您尝试将它们全部放入一个 Utility 类中,也许在某些情况下是可行的,但它很快就会变得丑陋并且难以维护。

关于ios - 在 iOS 中获取当前位置的静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22703427/

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