如何从(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)
        }
    }

  }

}

先感谢您..!!!

最佳答案

您可以定义闭包以便在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函数中调用的完成块时运行。

10-02 14:00