'No exact matches to call Initilizer in Swift Table View Cell configuration
I got some nil values are exist into API as found into console window . I marked the filed as option into model as well other view controller But still giving me same message . No exact matches in call to initializer
Storyboard Screenshot .
Here is my model .
import Foundation
// MARK: - Welcome
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"
}
}
Here is the presenter class.
import Foundation
import UIKit
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.venues
DispatchQueue.main.async {
self.view.resfreshTableView()
}
case .failure(let error):
DispatchQueue.main.async {
self.view.displayError(error.localizedDescription)
//self.view.displayError(error.errorDescription ?? "")
print(error)
}
}
}
}
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
}
}
Here is the view controller class.
import UIKit
class ViewController: UIViewController{
private var presenter : VenuePresenter!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
setUpUI()
presenter = VenuePresenter(view: self)
presenter.getVanue()
self.tableView.register(DisplayView.self, forCellReuseIdentifier: DisplayView.identifier)
}
private func setUpUI() {
tableView.dataSource = self
tableView.delegate = self
}
}
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() }
//tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DisplayView1")
let row = indexPath.row
guard let id = presenter.getId(by: row) else { return UITableViewCell()}
guard let lat = presenter.getLat(by: row) else { return UITableViewCell () }
guard let lon = presenter.getLon(by: row) else { return UITableViewCell() }
guard let createdOn = presenter.getCreated(by: row) else { return UITableViewCell() }
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 display view controller .
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 the screenshot of the error message .
Solution 1:[1]
have you tried using "(lat)" ?
label2.text = "\(lat)"
you also might want to unwrap the optionals since you are alsoi getting an error
label1.text = "\(id ?? "default")"
Solution 2:[2]
As you are using a storyboard with a prototype cell, you don't need to register your cell class in viewDidLoad.
You need to ensure that you have put the correct reuse identifier DisplayView in your prototype cell in your storyboard, set the cell's custom class and connected the outlets.
Solution 3:[3]
You are clearly using Prototype Cells and the error indicates that the identifier of the cell in Interface Builder is not set
When using Prototype Cells you must not register any cell. Delete
self.tableView.register(DisplayView.self, forCellReuseIdentifier: DisplayView.identifier)In the view represented by the screenshot select "Display View", then press ??5 to open the Attributes Inspector. Enter
DisplayViewin theIdentifierfield.
Side note: Never guard the cell in cellForRow. Force unwrap it
let cell = tableView.dequeueReusableCell(withIdentifier: DisplayView.identifier,
for: indexPath) as! DisplayView
The code must not crash. If it does you made a design mistake which can be fixed at once
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Blacksmith |
| Solution 2 | |
| Solution 3 | vadian |


