Post

Replies

Boosts

Views

Activity

Reply to Store Location from API into Array
						 				GoogleURLAPI = GoogleURLAPI.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! 				 				var urlRequest = URLRequest(url: URL(string: GoogleURLAPI)!) 				 				urlRequest.httpMethod = "GET" 				 				let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in 						if error == nil 						{//making sure it actually has something 								let jsonDict = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary 								//setting jsonDict to read datta from url 								if let LocArray = jsonDict?.value(forKey: "results") as? NSArray 								{ 										//print("or here",LocArray) 										for x in LocArray 										{ 												if let Location = x as? NSDictionary 												{ 														if let loc = Location.value(forKey: "location") 														{ 																		self.tester.append(loc as! String) 																		//self.nameArray.append(loc as! String) 														 														} 												} 										} 								} 								self.printNames() 								//print("json = \(jsonDict)") 								 						} 						else 						{ 								let alert = UIAlertController(title: "There was an Error", message: "We encountered an error while trying to connect to Google. Try again later.", preferredStyle: UIAlertController.Style.alert) 								alert.addAction(UIAlertAction(title: "Okay!", style: UIAlertAction.Style.default, handler: nil)) 								self.present(alert, animated: true, completion: nil) 						} 				} 				task.resume() 				 			
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Store Location from API into Array
This is the entire printed console when the code is ran: Console Out - https://developer.apple.com/forums/content/attachment/3c3a44c4-37ca-4c33-9789-deba028c4d53 And what I mean with switch values, when I change if let location = result["location"] as? String , to if let location = result["name"] as? String , I am able to fill the name array and gets printed: name array ["Roll N Noodle Food Court", "Jalisco Mexican Food", "True Food Kitchen", "Corazon Mexican Food", "Chick-fil-A", "Wendy\'s", "Jack in the Box", "Jack in the Box", "Chick-fil-A", "Jack in the Box", "Chick-fil-A", "Wendy\'s", "Wendy\'s", "Jora Peruvian food", "Jack in the Box", "Rubio\'s Coastal Grill", "sweetgreen", "Taqueria", "Arby\'s", "New Port Dim Sum and Chinese Food"]
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21