EurekaCommunity / GooglePlacesRow

This repository aims to create a row for Eureka with autocomplete from Google Places
MIT License
60 stars 12 forks source link

Getting the selected Place #7

Closed emadd closed 7 years ago

emadd commented 7 years ago

Could you provide an example of how to get the place that was selected from a table result set?

mats-claassen commented 7 years ago

You mean the place selected by the user from the table that appears with the options? The value of the row should be that place

emadd commented 7 years ago

Thanks! So it appears to return both the query text (userInput) or the selected place (prediction) . How do you tell the difference between the two?

mats-claassen commented 7 years ago

The value is of type GooglePlace which is an enum. If the user selected a value from the table view or collection view then the value will be a GooglePlace.prediction. If the user did not select anything but just left his written text in the row then the value will be a GooglePlace.userInput

emadd commented 7 years ago

Sorry, I'm not making myself clear enough. How exactly do I extract the GMSAutocompletePrediction from the GooglePlace?

let place: GooglePlace = row.value!
switch place {
    case .prediction:
        print("Location Selected")
        // TODO: extract the place
   case .userInput:
       print("userInput")
       // Do nothing
}
mats-claassen commented 7 years ago

The prediction or userInput is an associated value of the GooglePlace enum

public enum GooglePlace {
    case userInput(value: String)
    case prediction(prediction: GMSAutocompletePrediction)
}

So you can get it like this:

switch place {
            case let GooglePlace.userInput(val):
                return val
            case let GooglePlace.prediction(pred):
                return pred.attributedFullText.string
            }
calvinorchiz commented 7 years ago

With "let place: GooglePlace = row.value!" i got the following error: Cannot convert value of type 'GooglePlace' to specified type 'GooglePlace'