Home > Software engineering >  Unrecognized selector sent to instance 0x7fecc7011000" In Swift
Unrecognized selector sent to instance 0x7fecc7011000" In Swift

Time:02-12

I cannot figure it out why this error is happening unrecognized selector sent to instance 0x7fecc7011000" . I added the all the necessary code but cannot find which selector actually is messing .

Screenshot of the project structure with story board . enter image description here The network Manager code .

class NetworkManager {
    
    func getCoins(from url: String, completion: @escaping (Result<VanueResponse, NetworkError>) -> Void ) {
        
        guard let url = URL(string: url) else {
            completion(.failure(.badURL))
            return
        }
        
        URLSession.shared.dataTask(with: url) { data, response, error in

            if let error = error {
                completion(.failure(.other(error)))
                return
            }

            if let data = data {
                //decode
                do {
                    let response = try JSONDecoder().decode(VanueResponse.self, from: data)
                    completion(.success(response))
                } catch let error {
                    completion(.failure(.other(error)))
                }
            }
        }
        .resume()
    }
    
    
}

Presenter code ..

class VenuePresenter : VanueProtocol{

    // creating instance of the class
    private let view : VanueViewProtocol
    private let networkManager: NetworkManager
    private var vanues = [Venue]()
    var rows: Int{
        return vanues.count
    }
    // initilanize the class
    init(view:VanueViewProtocol , networkmanager:NetworkManager = NetworkManager()){
        self.view = view
        self.networkManager = networkmanager
    }
    
    
    func getVanue(){
        
        let url  = "https://coinmap.org/api/v1/venues/"
        
        networkManager.getCoins(from: url) { result in
            
            switch result {
            case.success(let respone):
                self.vanues = respone.results
                DispatchQueue.main.async {
                    self.view.resfreshTableView()
                }
            case .failure(let error):
                DispatchQueue.main.async {
                    self.view.displayError(error.localizedDescription)
                    print(Thread.callStackSymbols)
                }
            }
            
        }
    }
    
    
    

    func getId(by row: Int) -> Int {
        return vanues[row].id
    }
    
    func getLat(by row: Int) -> Double {
        return vanues[row].lat
    }
    
    func getCreated(by row: Int) -> Int {
        return vanues[row].createdOn
    }
    
    func getLon(by row: Int) -> Double? {
        return vanues[row].lon
    }
    
    
}

Protocol code ..

import Foundation

protocol VanueProtocol {
    func getVanue()
    func getId(by row: Int) -> Int
    func getLat(by row: Int) -> Double
    func getLon(by row: Int) -> Double?
    func getCreated(by row: Int) -> Int
    var rows: Int { get }
}

protocol VanueViewProtocol {
    func resfreshTableView()
   func displayError(_ message: String)
}

Here is the code in view controller .

class ViewController: UIViewController{

    
    private var presenter : VenuePresenter!
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self
        presenter = VenuePresenter(view: self)
        presenter.getVanue()
        
    }
    
}

extension ViewController : VanueViewProtocol{
    func resfreshTableView() {
        tableView.reloadData()
        
    }
       func displayError(_ message: String) {
            let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
            let doneButton = UIAlertAction(title: "Done", style: .default, handler: nil)
            alert.addAction(doneButton)
            present(alert, animated: true, completion: nil)
        }
    }
extension ViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        presenter.rows
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        guard let cell = tableView.dequeueReusableCell(withIdentifier: DisplayView.identifier, for: indexPath) as? DisplayView
          else { return UITableViewCell() }
        
        let row = indexPath.row
        let id   = presenter.getId(by: row)
        let lat = presenter.getLat(by: row)
         guard let lon = presenter.getLon(by: row) else { return UITableViewCell() }
        let createdon = presenter.getCreated(by: row)
        cell.configureCell(id: id, lat: lat, lon: lon, createdon: createdon)
        return cell
    }
}
extension ViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
}
    

Here is the code in custom view controller named is display view .

import UIKit

class DisplayView: UITableViewCell{
    
    static let identifier = "DisplayView"
    
    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label3: UILabel!
    @IBOutlet weak var label4: UILabel!
    
    func configureCell(id: Int ,lat : Double , lon : Double , createdon: Int){
        label1.text = String(id)
        label2.text = String(lat)
        label3.text = String(lon)
        label4.text = String(createdon)
    
    }
   
    
    
}
    
     

Here is my model .

struct Coin: Codable {
    let venues: [Venue]
}

// MARK: - Venue
struct Venue: Codable {
    let id: Int
    let lat, lon: Double
    let category, name: String
    let createdOn: Int
    let geolocationDegrees: String

    enum CodingKeys: String, CodingKey {
        case id, lat, lon, category, name
        case createdOn = "created_on"
        case geolocationDegrees = "geolocation_degrees"
    }
}

I added the break point but it retuning nil and it needs to return the data from API . Here is the API link .https://coinmap.org/api/v1/venues/ enter image description here Here is the screenshot of the error .

enter image description here

Here is the console window . console Window

CodePudding user response:

The problem is this outlet:

IBOutlet weak var tableView: UITableView!

You have hooked it up in the storyboard to the wrong object. It is hooked to the cell, not the table view. It looks like you must have hooked it up and then changed the class of the thing it is hooked to.

  • Related