Post

Replies

Boosts

Views

Activity

Reply to Improve SearchBar
This is what I have for my class CurrentPlayers. CurrentPlayers {   var photoUrl: String   var position: String   var team: String   var yahooName: String   var birthCity: String   var status: String       init(photoUrl: String, position: String, team: String, yahooName: String, birthCity: String, status: String) {           self.yahooName = yahooName     self.photoUrl = photoUrl     self.position = position     self.team = team     self.birthCity = birthCity     self.status = status         } } My parseJson func performs a get request for an API and it populates the array with JSON data. func parseJson() {     cPlayerArr = []     let url = //url I am using to get data from     var request = URLRequest(url: URL(string: url)!)     request.httpMethod = "GET"           let configuration = URLSessionConfiguration.default     let session = URLSession(configuration: configuration, delegate: nil, delegateQueue: OperationQueue.main)     let task = session.dataTask(with: request) { (data, response, error) in       if (error != nil) {         print("error")       } else {         do {           let fetchedData = try JSONSerialization.jsonObject(with: data!, options: .mutableLeaves) as! NSArray           for eachPlayerNow in fetchedData {             let eachPlayer = eachPlayerNow as! [String: Any]             let player = eachPlayer["YahooName"] as?             String             let position = eachPlayer["Position"] as? String             let photoUrl = eachPlayer["PhotoUrl"] as? String             let team = eachPlayer["Team"] as?             String             let birthCity = eachPlayer["BirthCity"] as? String             let status = eachPlayer["Status"]             as? String             self.cPlayerArr.append(CurrentPlayers.init(photoUrl: photoUrl ?? "", position: position ?? "", team: team ?? "", yahooName: player ?? "", birthCity: birthCity ?? "", status: status ?? ""))           }           self.collections.reloadData().			         }         catch {           print("error 2")         }       }     }     task.resume()   }
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to Improve SearchBar
for my print("search for: ", searchText, searchText.count) it shows the exact amount of characters and the word in the searchbar even when I delete characters. I added the parseJson() right before the filter and it paused it like you said and reset the array. I don't have a textField delegate. This is my cellforItemAt I think thats what you wanted because there is no itemAtRow. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colcell", for: indexPath) as! PlayerColCell           let item = cPlayerArr[indexPath.row]     cell.layer.borderColor = UIColor.black.cgColor     cell.layer.borderWidth = 0.5     cell.contentView.backgroundColor = UIColor(red: 102/256, green: 255/256, blue: 255/256, alpha: 0.66)           cell.update(with: item)           return cell        }
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to Improve SearchBar
It's not the search string and the array is complete and when parseJson() is run it shows all the cells. When I type E it shows less cells when I type n it shows even less cells, when I type f it shows even less cells but when I delete the f it shows 0 cells and when I delete n it again shows 0 cells so I have to delete everything to restart everything. This function shows the number of cells I have displayed.  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {           print("Number of cells:", cPlayerArr.count)     return cPlayerArr.count        }
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to Protocol Not Working
Btw this is all the code related to my protocol and what I am trying to transfer. As for the data on the results print("invoked HockeyDetailVC is", self) gives me invoked HockeyDetailVC is <LearnHockey.HockeyDetailVC: 0x7fe0b3026200>. Can you explain the 0x7fe0b3026200. print("The delegate is nil") gives me The delegate is nil. print("destination HockeyDetailVC is", hockeyDetailVC) This does not print anything to my console.
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Protocol Not Working
This is how I define the function   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {   if segue.identifier == "r" {     print("segue identifier", segue.identifier)     let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC     hockeyDetailVC.delegate = self     print("destination HockeyDetailVC is", hockeyDetailVC)   }  } I don't use an IB but I have an identifier I named r in the storyboard.
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Protocol Not Working
I can transfer the data using the prepare method like so in my hockeyDetailVC override func prepare(for segue: UIStoryboardSegue, sender: Any?) {     if segue.identifier == "r" {     let destinationController = segue.destination as! FavouritesVC     destinationController.currentFav = item!   }   } but I am still curious about why my protocol is not transferring the data I want to send. I copied    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {    if segue.identifier == "r" {      print("segue identifier", segue.identifier)      let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC      hockeyDetailVC.delegate = self      print("destination HockeyDetailVC is", hockeyDetailVC)    }   } It does not print the segue.identifier it prints the delegate is nil and then it just crashes again. It does not matter what I change my segue.identifier too.
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21