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
Reply to Array Only Shows Populated After Second Run Through
If you found something wrong and fixed it by yourself, please share your solution. There are some readers other than you and me, and more in the future. Sharing the solution would benefit all such readers, and me. There was just an extra "e" in DispatcheQueue.main.async { . Should read DispatchQueue.main.async { . Thank you so much for your help and clarification!
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Array Only Shows Populated After Second Run Through
The gotoplaces() function is what utilizes the arrays: func gotoaces()   {     printNames()     let number = Int.random(in: 0..<nameArray.count)     destination = locationArray[number]     let marker = GMSMarker()     marker.position = destination     marker.title = "Desitination"     marker.snippet = nameArray[number]     marker.map = self.mapView           self.mapView.camera = GMSCameraPosition(target: destination, zoom: 25)           getRouteSteps(from: origin, to: destination)         } I thought that since the function was called after task.resume() meant that it finished?
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Store Location from API into Array
Yes, that worked! But the array only shows it be populated after I run the function twice. Like, the first go through will print out two empty arrays and then when I tap the button to run the function again the array shows populated. When the print statement is within the do brackets, it shows it is filled, but once its after the task.resume(), it will only show a populated array after the second execution
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Store Location from API into Array
Sorry, this is the first part of the output leading up to "results": {   "htmlattributions" : [],   "nextpagetoken" : "ATtYBwJIt2W3qkkzyErZBsrUXDJg8XW9cImoZT2uhayI9OdU-eLnyVdBfLZUHMyGRXnuLSxNyxDFDquPpf7lUqP0gm9I5Qvim1FaxE2uewpCTV3J37bMxTcR0sw1FdIviWQiGbelrUuduR3iaWgyDsw3dNM26XtHun3z2twzc32K24wXNXYjMsgJI1jUKRdy9Os4uyjjwm287ovoQdvqe-swRrvb1omAB-303ytyk7o0TmtohgT0-7c0pCODwqMMZ19R9ISvu6j0SkcTc9960XJriCPy7-BmTJNz8BFzLsS15dc0SVAEttjC9is5ZEjiw0Oa2-yzbfZbvCHNDH-v3HdjlYQLOD6YV4Oog0iVgQM4T_7yTao-LhEcrSeQGIcZlvbHHs5fzBJiNPmm7p7sVCpxoxes3wKO3rSTzAOEm-MRzyKetCUZV3ZxU1KqhoQ2Wdj2mo",   "results" : [    {
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Store Location from API into Array
The print statement on line 21 prints out the google api page and then nothing else is printed until line 51, "value for location not found or not string". I did try change the array type to double but same output. When I switch, on line 46, the key value to "name" it is still able to store the values of name into the array and prints out properly. And yes, I'm attempting to teach myself how to program in swift and make an IOS app. I've been picking parts from different forums to try and make this app.
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Store Location from API into Array
This is the function. When on line 21, if let loc = Location.value(forKey: "location"), when the key is set to "name", the name array fills up with names from the google api, but when ever the value is set to location, it is always empty func printNames()   {     print("name array",nameArray)     print("location array",locationArray)   }
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Show array being traversed
Thank you for the tips! I'm attempting to teach myself how to make an app and that will certainly help with feature projects. I tried adding the code segment into my program, and when I tap my random button, it ends up crashing and throwing the error: Thread 1: "-[Foodapp.LazyViewController Random]: unrecognized selector sent to instance 0x7ffaa3415670" terminating with uncaught exception of type NSException
Jan ’21
Reply to Show array being traversed
Yes, I am trying to write an IOS app in Swift. It compiles and runs without any warnings or errors. My main goal is for the contents of the array to briefly show onto the label right before the random element that was selected is displayed. Pretty much like a randomizer does right before displaying the random element. The whole functions is: var prev = -1    @IBAction func Random()    {      let Places = ["Burger King","Chick-Fil-A","McDonald's","Wendys","Checkers","TacoBell","PDQ","Pizza Hut","Jimmy John's","Subway","Dominos","Pollo Tropical","Chipotle","Olive Garden","Ale House","Five Guys","Fresh Kitchen","Blaze","Outback"]            label.isHidden = false      var i = 0      var number = Int.random(in: 0..<19)            if(prev == number)      {        number = Int.random(in: 0..<19)      }      while i<Places.count      {        label.text = Places[i]        sleep(UInt32(0.99))        i+=1      }            label.text = Places[number]      prev = number    }
Jan ’21