Thanks for showing the definition.
But in your shown code, self.nameArray.append(loc as! String) is commented out, so it does not make sense.
Anyway, your code is full of bad practices and you should better update it.
Please try the following code and tell us what you get from print statements.
				//#1 Start non-type identifiers with lower case letter
				var googleURLAPI = URLComponents(string: "https://maps.googleapis.com/maps/api/place/nearbysearch/json")!
				//#2 Better not call `addingPercentEncoding` yourself
				googleURLAPI.queryItems = [
						URLQueryItem(name: "location", value: "\(origin.latitude),\(origin.longitude)"),
						URLQueryItem(name: "radius", value: "15000"),
						URLQueryItem(name: "type", value: "Fast Food"),
						URLQueryItem(name: "keyword", value: "Food"),
						URLQueryItem(name: "key", value: "API KEY"),
				]
				print(googleURLAPI.url!)
				
				var urlRequest = URLRequest(url: googleURLAPI.url!)
				
				urlRequest.httpMethod = "GET"
				
				let task = URLSession.shared.dataTask(with: urlRequest) {
						(data, response, error) in
						do {
								//making sure it actually has something
								if let error = error {
										throw error
								}
								//#3 Unwrap Optional safely with gurad-let
								guard let data = data else {
										print("data is nil")
										return // or throw some error
								}
								//For debugging, show response data as text
								print(String(data: data, encoding: .utf8) ?? "?")
								//#4 Better not use `try?`, use do-try-catch
								//#5 `mutableContainers` has no meaning in Swift
								//#6 Use Swift Dictionary type instead of `NSDictionary`
								guard let jsonDict = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] else {
										print("response data is not a JSON object")
										return // or throw some error
								}
								//setting jsonDict to read datta from url
								//#7 Use Swift Array type instead of `NSArray`
								guard let results = jsonDict["results"] as? [[String: Any]] else {
										print("`results` is not an Array of JSON object")
										return // or throw some error
								}
								//print("or here",LocArray)
								for result in results {
										if let location = result["location"] as? String {
												self.tester.append(location)
												//self.nameArray.append(location)
										} else {
												//#8 Show debug info handling else-case
												print("value for `location` not found or not string")
												return // or throw some error
										}
								}
								self.printNames()
								//print("json = \(jsonDict)")
						} catch {
								print(error)
								//#9 Better use `NSLocalizedString` for future internationalization
								let title = NSLocalizedString("There was an Error", comment: "")
								let message = NSLocalizedString("We encountered an error while trying to connect to Google. Try again later.", comment: "")
								let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
								//#10 You can omit `handler:` when passing nil
								alert.addAction(UIAlertAction(title: "Okay!", style: .default))
								self.present(alert, animated: true, completion: nil)
						}
				}
				task.resume()
Generally, your original code disposes error info and ignores many unexpected cases, and also using too many forced unwrappings (!) and forced-castings (as!). Have you picked up this code from a very old article?
Topic:
Programming Languages
SubTopic:
Swift
Tags: