- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 CoreLocation 成功确定用户的位置。但是,当我尝试使用 CLLocationManagerDelegate 方法时:
func locationManager(_ manager: CLLocationManager!, didFailWithError error: NSError!)
我遇到了错误术语的问题。
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("didFailWithError \(error)")
if let err = error {
if err.code == kCLErrorLocationUnknown {
return
}
}
}
这会导致“使用未解析的标识符 kCLErrorLocationUnknown”错误消息。我知道 kCLErrors 是枚举,它们在 Swift 中已经发展,但我被卡住了。
最佳答案
Swift 4 更新:错误现在作为 error: Error
传递给回调,可以转换为 CLError
:
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
if let clErr = error as? CLError {
switch clErr.code {
case CLError.locationUnknown:
print("location unknown")
case CLError.denied:
print("denied")
default:
print("other Core Location error")
}
} else {
print("other error:", error.localizedDescription)
}
}
较旧的答案:核心位置错误代码定义为
enum CLError : Int {
case LocationUnknown // location is currently unknown, but CL will keep trying
case Denied // Access to location or ranging has been denied by the user
// ...
}
并将枚举值与整数err.code
进行比较,toRaw()
可以使用:
if err.code == CLError.LocationUnknown.toRaw() { ...
或者,您可以根据错误代码创建一个 CLError
并检查对于可能的值:
if let clErr = CLError.fromRaw(err.code) {
switch clErr {
case .LocationUnknown:
println("location unknown")
case .Denied:
println("denied")
default:
println("unknown Core Location error")
}
} else {
println("other error")
}
更新:在 Xcode 6.1 beta 2 中,fromRaw()
和 toRaw()
方法已被分别由 init?(rawValue:)
初始化程序和 rawValue
属性替换:
if err.code == CLError.LocationUnknown.rawValue { ... }
if let clErr = CLError(rawValue: code) { ... }
关于Swift:Corelocation 处理 didFailWithError 中的 NSError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31219025/
我意识到这与此处的现有帖子相似,What's the Point of (NSError**)error? ,但我的问题有点不同。我了解双指针的工作原理,以及这是常见的 iOS API 错误模式。我的
我刚刚升级到 Xcode 6.3 和 Swift 1.2,并且正在更新语法。在我的项目中,我使用 TCBlobDownloadSwift用于一些常规下载任务。更新后,我从框架的以下方法中收到编译器错误
在下面的代码中NSError实例化(match是GameCenter的GKMatch): ... NSString * deviceID = appDelegate.deviceID;
我开发了一个 ios 应用程序,它有: NSError *error; 代替: NSError *error = nil; 当我在模拟器中调试和连接时在设备上调试时,它工作正常。在我将其存档并将其发
在我的 iOS 应用程序中,我需要播放声音。我有以下代码: var audioFileLocationUrl = NSBundle.mainBundle().pathForResource("midn
我正在使用外部组件连接到 SOAP 服务。当我收到结果时,它作为对象 value 返回,它可以包含 NSError 或结果对象。 如果出现错误,我想提取错误代码和消息。如何将 value 转换为 NS
我有一个 NSError ** 存储在一个数组中(所以我可以这样得到它 array[0])。我正在尝试将其转换为变量: NSError * __autoreleasing *errorPointer
我需要另一双眼睛。 奇怪的是,我似乎无法访问自定义 NSError 的属性。我不断收到 EXC_BAD_ACCESS 错误。这是我的代码: if (response.isUnautho
我收到这个错误: incompatible pointer types assigning to NSError _strong from NSError _autoreleasing 这是我的头文件
我正在 swift 中使用 AFNetworking,并且在将以下代码转换为 swift 时遇到问题 objective-c [client GET:url parameters:nil succes
physicsWorld.gravity = CGVector(dx: 0, dy: -2) physicsWorld.contactDelegate = self motionMan
这个问题类似于ios NSError types但是那里描述的解决方案没有用,我相信这不是我所需要的。 我有一个执行异步调用然后调用完成 block 的方法。当我尝试将 NSError ** 传递给完
我试图在 if let jsonData Array = try NSJSONSerialization... 行中从 Swift 1 转换为 2,因为它最初给了我一个来自“额外参数‘错误’的错误”
最近我刚刚将 swift 2.3 项目转换为 3.2,alamofire 也得到了转换,我收到了很多问题,其中大部分都已解决,现在我被给定的两个问题所困扰 问题在 alamofire 的 Respon
在我使用该方法之前几次... NSURLResponse *response; NSError *error; NSData *data = [NSURLConnection sendSynchron
这个相对简单的enum,编译得很好,但是...... public protocol AuthKitErrorProtocol { var error: NSError { get } } e
你好, 我正在尝试为接受 *NSError 作为参数的代码设置一些单元测试。如果存在验证问题,则不会保存对象并设置 NSError 条件。 我的方法是: - (BOOL)validateConsist
我想我在 NSError 上发现了一个错误,但想运行它让你们通过,看看是否真的只是我做错了什么。 在向使用 userInfo 的 NSError 实例发送描述时发生崩溃。如果将 userInfo 设置
对于这个问题,我使用的是我的实际代码的精简版本。 该应用程序使用 MVVM 架构构建。假设有一个登录屏幕。有3个文件。 ApiClient 文件与服务器通信并获取响应。如果输入的用户名和密码匹配,它会
例如 NSError *error = nil; if ([something error:&error]) { // ... } // ... if ([somethingElse er
我是一名优秀的程序员,十分优秀!