Ios 创建了一个函数,但在if语句中该函数为';我不认识斯威夫特
我制作了函数Ios 创建了一个函数,但在if语句中该函数为';我不认识斯威夫特,ios,swift,Ios,Swift,我制作了函数getLocation(),并在一个else{getLocation()}中使用了它,它可以工作。后来,当我试图定义一个新函数时,locationmanagerif…{getLocation()}得到错误: 使用未解析的标识符“getLocation” 我不确定语法是否正确,或者是否有其他方法调用getLocation函数 import UIKit import MapKit class ViewController: UIViewController, CLLocationMan
getLocation()
,并在一个else{getLocation()}
中使用了它,它可以工作。后来,当我试图定义一个新函数时,locationmanager
if…{getLocation()}得到错误:
使用未解析的标识符“getLocation”
我不确定语法是否正确,或者是否有其他方法调用getLocation
函数
import UIKit
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
var coreLocationManager = CLLocationManager()
var locationManager: LocationManager!
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var location_info: UILabel!
@IBAction func Update_Location(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
coreLocationManager.delegate=self
locationManager = LocationManager.sharedInstance
let authorizationCode = CLLocationManager.authorizationStatus()
if authorizationCode == CLAuthorizationStatus.notDetermined &&
coreLocationManager.responds(to: #selector(CLLocationManager.requestWhenInUseAuthorization))||coreLocationManager.responds(to: #selector(CLLocationManager.requestAlwaysAuthorization)){}
if Bundle.main.object(forInfoDictionaryKey: "NSlocationWhenInUseUsageDescription") != nil{
coreLocationManager.requestWhenInUseAuthorization()
}else{
getLocation()
}
}
func getLocation(){
locationManager.startUpdatingLocationWithCompletionHandler{(latitude, longitude, status, verboseMessage, error)->()in
self.displayLocation(location: CLLocation(latitude: latitude, longitude: longitude))
}
}
func displayLocation(location:CLLocation){
mapView.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude),span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)), animated:true)
let locationPinCoord = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let annotation = MKPointAnnotation()
annotation.coordinate = locationPinCoord
mapView.addAnnotation(annotation)
mapView.showAnnotations([annotation], animated: true)
locationManager.reverseGeocodeLocationWithCoordinates(location) { (reverseGecodeInfo, placemark, error) in
print(reverseGecodeInfo!)
}
}
}
func locationmanager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus){
if status != CLAuthorizationStatus.notDetermined || status != CLAuthorizationStatus.denied || status != CLAuthorizationStatus.restricted {
getLocation()
}
}
帮自己一个忙,使用适当的缩进
试试这个,全选,剪切,然后粘贴。Xcode将为您自动缩进,您将立即看到问题是您的
}
位置错误。不是这样,问题是函数没有被识别出来。是这样的。我复制并粘贴了你的代码,所以我知道了。您还有其他问题,但大括号是找不到getLocation()
的原因。@Shannon您缺少的是didChangeAuthorizationStatus
delegate方法不在类中,因为您可以在代码正确缩进时看到它。您的getLocation()
嵌套在viewDidLoad()
的主体中,因此它仅在viewDidLoad()中可用。