UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

I am stuck on project 7 challenge 2

Forums > 100 Days of Swift

I am stuck on challenge two, I can't seem to figure out how to place filtered petitions aka the petitions that match the user's string input, into the filtered arrays. I've been at it for hours with no success. The .append() method refuses to work when I try to append word(user's input) into the filtered array. Here's my code, I appreciate all the help.

""" import UIKit

class ViewController: UITableViewController { var petitions = [Petition]() var filteredPetitions = [Petition]()

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(credits))
    navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(enterText))

    let urlString: String

    if navigationController?.tabBarItem.tag == 0{
        urlString = "https://www.hackingwithswift.com/samples/petitions-1.json"
    }
    else{
        urlString = "https://www.hackingwithswift.com/samples/petitions-2.json"

    }
    if let url = URL(string: urlString){
        if let data = try? Data(contentsOf: url){
            //if we are here, that means all the if lets succeeded and we can parsa
            parse(json: data)
            return
        }

        showeError()
    }
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return petitions.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    let petition = petitions[indexPath.row]
    cell.textLabel?.text = petition.title
    cell.detailTextLabel?.text = petition.body
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = DetailViewController()
    vc.detailItem = petitions[indexPath.row]
    navigationController?.pushViewController(vc, animated: true)
}

func parse(json: Data){
    let decoder = JSONDecoder()

    if let jsonPetitions = try? decoder.decode(Petitions.self, from: json){
        petitions = jsonPetitions.results
        tableView.reloadData()
    }
}

func showeError(){
    let ac = UIAlertController(title: "Loading Error", message: "There was a problem loading the feed", preferredStyle: .alert)
    ac.addAction(UIAlertAction(title: "Ok", style: .default))
    present(ac,animated: true)
}

@objc func credits(){ let Credits = URL(string: "https://www.hackingwithswift.com")

   let ac = UIAlertController(title: "Petitions credits", message: "This petition came from: \(Credits!)", preferredStyle: .alert)
   ac.addAction(UIAlertAction(title: "OK", style: .default))
   present(ac,animated: true)
}

@objc func enterText(){
    let ac = UIAlertController(title: "enter word", message: nil, preferredStyle: .alert)
    ac.addTextField()
    let submitAction = UIAlertAction(title: "Submit", style: .default){
        [weak self, weak ac] action in
        guard let word = ac?.textFields?[0].text else{return}
        self?.submit(word)

    }
    ac.addAction(submitAction)
    present(ac, animated: true)
}

func submit(_ word: String){
    filteredPetitions.removeAll(keepingCapacity: true)
    if petitions.contains(where: {$0.title == word}){
        if petitions.contains(where: {$0.body == word}){

            filteredPetitions.apend()
            tableView.reloadData()

        }
    }
}

}

"""

3      

Please.

Please put all your code inbetween the markup tags. Some of your code is outside the back ticks.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.