0

I have this piece of code and I want to save the locations which I get from the MKLocalSearch in an array, so I can use them later. Do you have any ideas how I can do that?

func searchForBarsAndRestaurants(searchFor: String){
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = typeOfPlace   //or whatever you're searching for

    request.region = MKCoordinateRegionMakeWithDistance(location.coordinate, 300, 300)


    let search = MKLocalSearch(request: request)
    search.start { response, error in
        guard let response = response
        else {
            print("There was an error searching for: \(String(describing: request.naturalLanguageQuery)) error: \(String(describing: error))")
            return
        }

        print(response.mapItems.count)
        print("There are \(response.mapItems.count)" , searchFor)
        for item in response.mapItems {
            // You may be able to match the address to what the geoCode gives you
            // or present the user with a list of options
            print("\(String(describing: item.name))")
            var totalDistances: Array<Double> = Array()
            let distance = self.location.distance(from: item.placemark.location!)
            totalDistances += [distance]
            print("distance is " ,distance)
            print(totalDistances.count)
        }
    }
}
Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52

2 Answers2

1

Sure, you've just to use a singleton.

What you want is use a global variable. Here is an example of how to do that, w

let sharedNetworkManager = NetworkManager(baseURL: API.baseURL)

class NetworkManager {

    // MARK: - Properties

    let baseURL: URL

    // Initialization

    init(baseURL: URL) {
        self.baseURL = baseURL
    }

}
Danyl
  • 2,012
  • 3
  • 19
  • 40
0

You need to define totalDistances as Class Property.

var totalDistances: Array<Double> = Array()

func searchForBarsAndRestaurants(searchFor: String){
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = typeOfPlace   //or whatever you're searching for

    request.region = MKCoordinateRegionMakeWithDistance(location.coordinate, 300, 300)


    let search = MKLocalSearch(request: request)
    search.start { response, error in
        guard let response = response
        else {
            print("There was an error searching for: \(String(describing: request.naturalLanguageQuery)) error: \(String(describing: error))")
            return
        }

        print(response.mapItems.count)
        print("There are \(response.mapItems.count)" , searchFor)
        for item in response.mapItems {
            // You may be able to match the address to what the geoCode gives you
            // or present the user with a list of options
            print("\(String(describing: item.name))")
            let distance = self.location.distance(from: item.placemark.location!)
            self.totalDistances += [distance]
            print("distance is " ,distance)
            print(self.totalDistances.count)
        }
    }
}
Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52