【问题标题】:Swift 2 CLLocationManager ErrorSwift 2 CLLocationManager 错误
【发布时间】:2015-11-04 23:31:20
【问题描述】:

看过之前的帖子,但没有一个解决方案适合我。

我收到此错误:

Objective-C 方法 'locationManager:didUpdateLocations:' 由提供 方法 'locationManager(_:didUpdateLocations:)' 与 可选要求方法 'locationManager(_:didUpdateLocations:)' 在协议“CLLocationManagerDelegate”中

使用以下代码:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.last
    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
    self.mapView.setRegion(region, animated: true)
    self.locationManager.stopUpdatingLocation()
}

【问题讨论】:

  • 只是为了确认一下,你是否包含了coreLocation框架import CoreLocation
  • 投票结束,因为不可复制

标签: swift swift2 cllocationmanager cllocation


【解决方案1】:

您的问题无法重现。这是我机器上 iOS 项目中 Swift 文件的完整内容;它在 Swift 2.1 下编译非常愉快:

import UIKit
import CoreLocation
import MapKit
class MapViewController : UIViewController, CLLocationManagerDelegate {
    var mapView : MKMapView!
    var locationManager : CLLocationManager!
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations.last
        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
        self.mapView.setRegion(region, animated: true)
        self.locationManager.stopUpdatingLocation()
    }
}

当然,该代码实际上不会做任何事情(没有实际的地图视图);这只是一些代码。但是没有编译错误。

如果您仍在使用早期版本的 Swift,请更新您的 Xcode 副本。如果你使用的是 Xcode 7.1,你的错误是莫名其妙的。

【讨论】:

    猜你喜欢
    • 2015-09-11
    • 2014-11-15
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    相关资源
    最近更新 更多