作者:爲妳奮鬥壹輩子丶故事_932 | 来源:互联网 | 2023-01-31 21:04
如何从(userLocation.swift)中的一个类(GPSLocation)获取定位结果,并在文件(target.swift)中的另一个类中使用它们?
我需要以下国家/地区的国家/地区代码,城市,经度和纬度:
userLocation.swift
import UIKit
import MapKit
class GPSLocation {
func getGPSLocation(completion: () -> Void) {
let locManager = CLLocationManager()
var currentLocation: CLLocation!
locManager.desiredAccuracy = kCLLocationAccuracyBest
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways) {
currentLocation = locManager.location
let latitude = String(format: "%.7f", currentLocation.coordinate.latitude)
let lOngitude= String(format: "%.7f", currentLocation.coordinate.longitude)
let location = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
fetchCountryAndCity(location: location) { countryCode, city in
// debugPrint("Country:", countryCode)
// debugPrint("City:", city)
}
// debugPrint("Latitude:", latitude)
// debugPrint("Longitude:", longitude)
}
}
//MARK: Find countryCode & City name from longitude & latitude
func fetchCountryAndCity(location: CLLocation, completion: @escaping (String, String) -> ()) {
CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in
if let error = error {
debugPrint(error)
} else if let countryCode = placemarks?.first?.isoCountryCode,
let city = placemarks?.first?.locality {
completion(countryCode, city)
}
}
}
}
并将它们打印在文件中:
target.swift
import Foundation
class Post {
fileprivate func Post() {
func getLocation() {
// Get user GPS location (longitude, latitude, Country Code, City)
let getUserLocation = GPSLocation()
getUserLocation.getGPSLocation {
debugPrint("Country:", countryCode)
debugPrint("City:", city)
debugPrint("Latitude:", latitude)
debugPrint("Longitude:", longitude)
}
}
}
}
先感谢您..!!!
1> Maryam Fekri..:
您可以定义一个闭包,以在获取您的GPSLocation
类的位置后运行。您可以通过两种方式实现它:
您可以在您的GPSLocation
类中具有一个块代码变量,如下所示:
typealias completiOnHanlder= (_ lat: String, _ lng: String) -> Void
var completion: completionHanlder?
然后在Post
实例化后的类中GPSLocation
可以传递如下代码块:
getUserLocation.completion = {
// do any stuff here
}
或者,您可以将代码块传递给getGPSLocation
函数。这是如何重新定义您的功能:
func getGPSLocation(completion: (_ lat: String, _ lng: String) -> Void) {
let locManager = CLLocationManager()
var currentLocation: CLLocation!
locManager.desiredAccuracy = kCLLocationAccuracyBest
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways) {
currentLocation = locManager.location
let latitude = String(format: "%.7f", currentLocation.coordinate.latitude)
let lOngitude= String(format: "%.7f", currentLocation.coordinate.longitude)
let location = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
fetchCountryAndCity(location: location, completion: { countryCode, city in
delegate?.fetchedLocationDetails(location: location, countryCode: countryCode, city: city)
}) { delegate?.failedFetchingLocationDetails(error: $0) }
debugPrint("Latitude:", latitude)
debugPrint("Longitude:", longitude)
completion(latitude, longitude) // your block of code you passed to this function will run in this way
}
}
这将是您的Post
课程:
class Post {
func getLocation() {
// Get user GPS location (longitude, latitude, Country Code, City)
let getUserLocation = GPSLocation()
getUserLocation.getGPSLocation { (lat, lng) in
debugPrint("Latitude:", lat)
debugPrint("Longitude:", lng)
// HERE I NEED to PRINT longitude, latitude, Country Code, City
}
}
}
getGPSLocation
当我在上面重写您的函数时,只要在函数中调用了完成块,您放置在此处的闭包就会运行。