1

I'm new to swift and I was just working on this program that has a table view with a list of names that the user can search through. It was working fine, until I tried to implement a UISearchbar and it gave me the error

Failed to set (keyPath) user defined inspected property on (UITableViewCellContentView): [<UITableViewCellContentView 0x7fb4624048c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath.

I've done some research and I'm figured out that this error normally occurs when there is a problem with IB Outlets, but my outlets seem to be doing fine and this is what they look like-

Photo of my Outlet Connections

If you think there might be a problem in my code- here it is (Sorry it is messy):

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
    if searchBar.text == "" {
        searchBar.placeholder = "Enter Name"
        return true
    } else {
        return true
    }
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.endEditing(true)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searching == true {
        return searchingPeople.count
    } else {
    return people.count
    }

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tblView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    if searching == true {
        cell.textLabel?.text = searchingPeople[indexPath.row]
    } else {
        cell.textLabel?.text = people[indexPath.row]

    }

    return cell
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    searchingPeople = people.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()})
    searching = true
    tblView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    view.endEditing(true)
    searching = false
    searchBar.text = ""

    tblView.reloadData()
}



var people: [String] = []
var searchingPeople = [String()]
var searching = false

@IBOutlet weak var tblView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!





override func viewDidLoad() {

    super.viewDidLoad()
    tblView.delegate = self
    tblView.dataSource = self
    searching = false
    searchBar.text = ""

    guard let path = Bundle.main.path(forResource: "finalDataPapa", ofType: "json") else { return }
    let url = URL(fileURLWithPath: path)
    do {
        let data = try Data(contentsOf: url)
        let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
       // print(json)
        guard let array = json as? [Any] else { return }

//38104 people
        var i: Int = 1

        while i < 38104 {
            guard let personDict = array[i] as? [String: Any] else { return }
                guard let personfName = personDict["FIRST NAME"] as? String else { print("Persons name in numbers"); return }
                guard let personlName = personDict["LAST NAME"] as? String else { print("Persons name in numbers"); return }
             //guard let personmName = personDict["MIDDLE"] as? String else { print("Persons name in numbers"); return }
            let fullName = "\(personfName) \(personlName)"
            people.append(fullName)
            i = i + 1




        }
        DispatchQueue.main.async {
            self.tblView.reloadData()

        }


    }
    catch {
        print(error)

    }


class People: Decodable {

let LASTNAME: String?
let FIRSTNAME: String?
let MIDDLE: String?




enum CodingKeys: String, CodingKey {

case FIRSTNAME = "FIRST NAME"
case LASTNAME = "LAST NAME"
case MIDDLE = "MIDDLE"
}
}
}
}
  • 1
    You are using `UIViewController` but you need a `TableViewController`, you can add it on your storyboard. – Eric May 28 '20 at 21:18
  • Thank you, that worked! – chinachina123 May 28 '20 at 21:26
  • 1
    @chinachina123 Granted, if you want other items like say a CollectionView along with your TableView, you would need to use a ViewController and just set the delegate and dataSource of the TableView to your ViewController. – Julian May 28 '20 at 21:55

0 Answers0