1

I have the code below:

import UIKit

class SearchTableViewController: UITableViewController, UISearchBarDelegate{

    final let urlString = "http://tvshowapi.azurewebsites.net/tvshownewsfeed"

    @IBOutlet var tableView: UITableView!

    var nameArray = [String]()
    var favouriteCountArray = [String]()
    var imgURLArray = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        createSearchBar()

        self.downloadJsonWithURL()

    }

    func createSearchBar() {
        let searchBar = UISearchBar()
        searchBar.showsCancelButton = false
        searchBar.placeholder = "Search a show"
        searchBar.delegate = self
        searchBar.backgroundColor = UIColor.black
        searchBar.barTintColor = UIColor.black
        self.navigationItem.titleView = searchBar
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.black

        let searchTextField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField

        searchTextField?.textAlignment = NSTextAlignment.left
    }

    func downloadJsonWithURL() {
        let url = NSURL(string: urlString)
        URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in
            if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [[String:Any]] {

                for tvshow in jsonObj! {
                    if let name = tvshow["screenName"] as? String {
                        self.nameArray.append(name)
                    }
                    if let cnt = tvshow["favouriteCount"] as? Int {
                        self.favouriteCountArray.append("\(cnt)")
                    }
                    if let image = tvshow["imageUrl"] as? String {
                        self.imgURLArray.append(image)
                    }
                }


                OperationQueue.main.addOperation({
                    self.tableView.reloadData()
                })
            }
        }).resume()
    }

    func downloadJsonWithTask() {

        let url = NSURL(string: urlString)

        var downloadTask = URLRequest(url: (url as? URL)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 20)

        downloadTask.httpMethod = "GET"

        URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in

            let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)

            print(jsonData!)

        }).resume()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return nameArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! SearchTableViewCell
        cell.nameLabel.text = nameArray[indexPath.row]
        cell.dobLabel.text = favouriteCountArray[indexPath.row]

        let imgURL = NSURL(string: imgURLArray[indexPath.row])

        if imgURL != nil {
            let data = NSData(contentsOf: (imgURL as? URL)!)
            cell.imgView.image = UIImage(data: data as! Data)
        }

        return cell
    }


}

I'm getting three errors by Xcode:

  1. Cannot override with a stored property 'tableView'
  2. Getter for 'tableView' with Objective-C selector 'tableView' conflicts with getter for 'tableView' from superclass 'UITableViewController' with the same Objective-C selector
  3. Setter for 'tableView' with Objective-C selector 'setTableView:' conflicts with setter for 'tableView' from superclass 'UITableViewController' with the same Objective-C selector

SearchTableViewController Following is the code for the TableViewCell

class SearchTableViewCell: UITableViewCell {

    @IBOutlet weak var imgView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var dobLabel: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

What am I doing wrong?

Jonathan Simonney
  • 585
  • 1
  • 11
  • 25
Evangeli
  • 11
  • 1
  • 3
  • 2
    A `UITableViewController` already has a `tableView` property, and it is "automatically" connected. There should be no need to define your own `tableView` property. Compare http://stackoverflow.com/questions/34565570/conforming-to-uitableviewdelegate-and-uitableviewdatasource-in-swift. – Martin R Mar 24 '17 at 07:53
  • just rename tableView with @IBOutlet var ttvshowTbl: UITableView! and remove the "weak" from UITableViewCell. now clean and run – MAhipal Singh Mar 24 '17 at 08:04
  • tableView is the inbuilt property of the apple api, it is actually conflicting with your name and Apple property, you have to just change the name of the property, clean your project and run again the project. – Anil Kumar Mar 24 '17 at 08:07

3 Answers3

1

The UITableViewController class has an implicit tableView property with connected data source and delegate. Delete the IBOutlet and use that.

vadian
  • 274,689
  • 30
  • 353
  • 361
0

Xcode is basically telling you "am using that name already". Change the name to anything else like tableView2 or something and it will work because Xcode gets confused with similar names like you cant name an object "String" it will give an error too. I hope in the future this would help someone.

Ekkogaming
  • 32
  • 8
0

this error concern to the names of objects like imageView or Button or label to fix this error . Just rename the objects instead of imageView into restaurantImage or label into RestaurantLabel or button into RestaurantButton I wish you understand that .

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 29 '21 at 10:35