I'm trying to gather information from a place using Google API and the printed out value is: Optional("Website URL"). Is there anyway to remove the word optional and just use the URL of the place?
Code Block @IBAction func Info(_ sender: Any) { var website = "https://www.google.com/" //setting default website let googlePlaceURLAPI = URLComponents(string: "https://maps.googleapis.com/maps/api/place/details/json?place_id=\(placeArray[number])&fields=name,rating,formatted_phone_number,website&key=API_KEY")! print(googlePlaceURLAPI.url!) let placeID = placeArray[number] //An array that holds the placeIDs obtained from different place API to access this API // Specify the place data types to return. let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.website.rawValue)) let placesClient = GMSPlacesClient() placesClient.fetchPlace(fromPlaceID: placeID, placeFields: fields, sessionToken: nil, callback: { (place: GMSPlace?, error: Error?) in if let error = error { print("An error occurred: \(error.localizedDescription)") return } if let place = place { print("The selected place is: \(String(describing: place.website ))") } }) let vc = SFSafariViewController(url: URL(string: website)!) present(vc, animated: true) }
Assuming you get this output from print("The selected place is: \(String(describing: place.website ))"),the printed out value is: Optional("Website URL").
you may need to unwrap Optional in a safe manner:
Code Block if let place = place, let website = place.website { print("The selected place is: \(website)") }
This is not a critical issue, but you should better use error instead of error.localizedDescription to print debugging info.
Code Block if let error = error { print("An error occurred: \(error)") return }
print("....\(error)") will give you more info than error.localizedDescription.