关键词:
【中文标题】核心位置代表闭包中的价值【英文标题】:Core-location delegates value in Closures 【发布时间】:2017-01-09 06:47:08 【问题描述】:您好,我已经在 Swift 3 中实现了核心位置管理器,我想构建一个方法来从其委托中捕获位置值并返回闭包中的值,但无法这样做。任何帮助将不胜感激。
我的代码:
import Foundation
import CoreLocation
class LocationManager : NSObject, CLLocationManagerDelegate
var location: CLLocation?
var locationManager : CLLocationManager?
var currentLocation : CLLocationCoordinate2D?
var latitude : String?
var longitude : String?
var lat : Float?
var lon : Float?
// var locationManagerCallback : (Float?,Float?,String?,String?) ->()
static let sharedInstance : LocationManager =
let instance = LocationManager()
return instance
()
// MARK: - CLLocation Fetch Methods
func fetchLocationWithCompletionHandler(completion : @escaping(Float?,Float?,String?,String?) -> ()) -> Void
if locationManager != nil
locationManager?.stopUpdatingLocation()
locationManager?.delegate = nil
locationManager = nil
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.distanceFilter = kCLLocationAccuracyNearestTenMeters
locationManager?.desiredAccuracy = kCLLocationAccuracyBest
let requestWhenInUseSelector = NSSelectorFromString("requestWhenInUseAuthorization")
let requestAlwaysSelector = NSSelectorFromString("requestAlwaysAuthorization")
if (locationManager?.responds(to: requestWhenInUseSelector))!
locationManager?.requestWhenInUseAuthorization()
else if (locationManager?.responds(to: requestAlwaysSelector))!
locationManager?.requestAlwaysAuthorization()
locationManager?.startUpdatingLocation()
func requestWhenInUseAuthorization() -> Void
let status : CLAuthorizationStatus = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.denied || status == CLAuthorizationStatus.restricted
// TODO : Handle Denial Here
else if status == CLAuthorizationStatus.notDetermined
locationManager?.requestWhenInUseAuthorization()
func requestAlwaysAuthorization() -> Void
let status : CLAuthorizationStatus = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.denied || status == CLAuthorizationStatus.restricted
// TODO : Handle Denial Here
else if status == CLAuthorizationStatus.notDetermined
locationManager?.requestAlwaysAuthorization()
func reveseGeocode(currentLocation : CLLocation, completionCallback : @escaping(CLPlacemark?,Error?) -> Swift.Void) -> Void
let reverseGeocoder : CLGeocoder = CLGeocoder()
reverseGeocoder.reverseGeocodeLocation(currentLocation) (placemarkArray, error) in
let placemark : CLPlacemark = (placemarkArray?.first)!
completionCallback(placemark,error)
// MARK: - CLLocation Delegate Methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
self.location = locations[0]
currentLocation = self.location?.coordinate
lat = Float((currentLocation?.latitude)!)
lon = Float((currentLocation?.longitude)!)
latitude = String(format: "%lf",lat!)
longitude = String(format : "%lf",lon!)
UserDefaults.standard.set(lat, forKey: CURR_LAT_VAL)
UserDefaults.standard.set(lon, forKey: CURR_LON_VAL)
UserDefaults.standard.set(latitude, forKey: CURR_LAT_STR)
UserDefaults.standard.set(longitude, forKey: CURR_LON_STR)
UserDefaults.standard.synchronize()
print("\(lat)\(lon)\(latitude)\(longitude)")
// locManager(lat,lon,latitude,longitude)
func locationManagerDidPauseLocationUpdates(_ manager: CLLocationManager)
print("location manager did pause")
func locationManagerDidResumeLocationUpdates(_ manager: CLLocationManager)
print("location manager did resume")
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
print("Location fetch failed with error : \(error.localizedDescription)")
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
print("Authorization Changed")
【问题讨论】:
【参考方案1】:您需要将 block 变量保存在 LocationManager
类中,如下所示:
1。在你的类中添加变量:
var locationManagerCallback: ((Float?,Float?,String?,String?) -> ())?
2。从开始请求时保存完成块/关闭:
func fetchLocationWithCompletionHandler(completion: @escaping(Float?,Float?,String?,String?) -> ()) -> Void
locationManagerCallback = completion
...
3。从委托方法调用闭包:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
locationManagerCallback?(lat, lon, latitude, longitude)
以下是如何在其他类中使用它:
LocationManager.sharedInstance.fetchLocationWithCompletionHandler (lat, long, latitude, longitude) in
print("\(lat)\(long)\(latitude)\(longitude)")
希望对你有帮助。
【讨论】:
更新核心数据中的价值
】更新核心数据中的价值【英文标题】:UpdatingValueinCoreData【发布时间】:2009-11-0406:46:49【问题描述】:我正在尝试编写一个简单的库存检查程序,并且我有一个绑定到核心数据的表视图。该表有3列:型号、价格和库存数量。我... 查看详情
ARKit 变换矩阵中的值代表啥?
...中,我了解到矩阵包含有关节点当前平移、缩放、旋转和位置的信息。我不明白哪些价值观与这四件事特别相关。例如 查看详情
angular中代表啥意思
参考技术A这个是js中的闭包写法。闭包是什么?函数内部定义函数,连接函数内部和外部的桥梁闭包的作用有2个:一是前面提到的读取函数内部的变量,二是让这些变量的值保存在内存中,实现数据共享类似这样写法:(function()va... 查看详情
Windows 核心音频 API 中的 ERoles 枚举值代表啥?它们是相互排斥的吗?
】Windows核心音频API中的ERoles枚举值代表啥?它们是相互排斥的吗?【英文标题】:WhatdotheERolesenumerationvaluesinWindowsCoreAudioAPIsrepresent?Aretheymutuallyexclusive?Windows核心音频API中的ERoles枚举值代表什么?它们是相互排斥的吗?【发布时... 查看详情
viewDidLoad 之外的闭包
...码包含重复的行view.addSubview(label)并将标签放置在错误的位置。你能帮我解决这个问题吗?classHomeVC:UIViewC 查看详情
核心文件存储在 lxc 容器中的啥位置?
】核心文件存储在lxc容器中的啥位置?【英文标题】:Wherearecorefilesstoredinalxccontainer?核心文件存储在lxc容器中的什么位置?【发布时间】:2015-05-2405:41:05【问题描述】:当我在一个核心容器上运行代码时,我无法在我的容器上找... 查看详情
Swift 3 - 核心数据一对一关系获取价值
】Swift3-核心数据一对一关系获取价值【英文标题】:Swift3-CoredataOne-to-Onerelationgetvalue【发布时间】:2018-02-1916:27:46【问题描述】:我的核心数据中有一对一的关系,使用UIAlertAction我正在保存一些数据,并且它成功地保存到核心... 查看详情
javascript语言核心--高阶函数及闭包
标记及清除的内存管理方案解决了循环引用带来的内存泄漏倾向于认为"闭包"是保存保存变量的作用域在嵌套函数定义中,引用了外层函数的变量,此时闭包才会出现(结合Chrome devptools查看)立即执行表达式配合闭包创建私有... 查看详情
模拟中的核心定位问题
】模拟中的核心定位问题【英文标题】:corelocationpropleminsimulator【发布时间】:2012-10-2309:36:39【问题描述】:我无法在iphone模拟器中获取当前位置。我将自定义位置设置为埃及的纬度和经度,但没有获得当前位置。-(void)viewDidLoad... 查看详情
简单理解闭包
...知道在JavaScript中,变量的作用域是由它所在原代码中的位置决定的,并且嵌套的函数可以访问到它外面的变量functioninit(){varname="rongyang";functiondisplayname(){alert(name);}displayname(); } 查看详情
核心位置 - iphone sdk 中的准确性问题
】核心位置-iphonesdk中的准确性问题【英文标题】:CoreLocation-Accuracyprobleminiphonesdk【发布时间】:2011-05-1917:28:03【问题描述】:我在50米范围内的两台设备中安装了一个应用程序。应用程序在2000英尺内找到附近的在线用户。但是... 查看详情
揭秘:智邦国际erp系统核心价值
ERP系统将企业实际管理需求与先进信息技术完美结合,打造全程一体化管理体系,打破各部门、各区域、各系统之间沟通和协作的壁垒,建立规范、灵捷、高效的业务流程,实现销售、用户、项目、生产、库存、采购、... 查看详情
目标c中的单例类
...:26:38【问题描述】:我已经制作了一个单例类,用于通过核心位置获取位置。现在我的问题是我想知道位置何时更新。我想在此使用代表而不是通知。我知道我可以发布通知。但我根本不想使用通知。有没有其他方法可以做到这... 查看详情
go匿名函数与闭包的使用(代码片段)
...名函数,同时调用func()fmt.Printf("a=%d,str=%s",a,str)()//后面的()代表调用匿名函数//带参数的匿名函数f3:=func(i,jint)fmt.Printf("i=%d,j=%d",i,j)f3(1,2)//定 查看详情
函数高阶应用与闭包(代码片段)
...内存地址<functionfuncat0x000001AE33574040>#如果带上括号则代表是函数func执行后return的结果f1=funcf1()#====执行结果===="""functionfunc..."""#====可以把函数当做参数传入另一个函数====deffoo(func):func()defbar():print("functionbar..")foo(bar)#代表将bar... 查看详情
通证经济最核心的价值,就是带来了流动性的质变
区块链的一个重要的落地应用是通证经济。以比特币、Libra、央行数字货币为代表的数字通证开始出现,货币迎来了数字化时代。朱嘉明等学者判断:数字通证已经经历了两个阶段:在第一阶段,数字通证需要与... 查看详情
urlsrc路径中的./..//
一:简明扼要"./"代表文件所在的位置“../”代表上升到文件所在位置的存放文件夹的水平层级“/”代表根目录所在的位置不明白?上案例二案例以上表中文件路径问题.html次文件作演示1要在文件路径问题.html中引... 查看详情