'No exact matches in call to initializer error when making a structure with a table view
I am new to coding structures and this is for my school project. I am trying to make a structure with a tableview and I keep on getting the error "No exact matches in call to initializer ."
I have added my whole code in order provide a full picture of my project. My aim with this code is to be able to input a structure of my friends. Each line must include their name, age and best friend status. This must be completed with a structure and please bare with me as I am very new to this.
Thank you for your help!
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friends.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//ensure the cell identifier has been labelled "cell"
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = String(friends[indexPath.row]) **//error occurs here**
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tblFriends.delegate = self
tblFriends.dataSource = self
}
// Outlets
@IBOutlet weak var txtName: UITextField!
@IBOutlet weak var txtAge: UITextField!
@IBOutlet weak var swiBestFriendsList: UISwitch!
@IBOutlet weak var tblFriends: UITableView!
@IBOutlet weak var lblMessage: UILabel!
struct Friend{
let name: String
let age: Int
let bestFriend: Bool
}
var friends: [Friend] = []
// Actions
@IBAction func btnAddFriend(_ sender: Any) {
if Int(txtAge.text!) != nil && txtAge.text != ""{
if (txtName.text!) != "" {
let newFriend = Friend(name: txtName.text!, age: Int(txtAge.text!)!, bestFriend: swiBestFriendsList.isOn)
friends.append(newFriend)
print(friends)
txtAge.text = ""
txtName.text = ""
if swiBestFriendsList.isOn{
lblMessage.text = "Best Friend has been added"
tblFriends.reloadData()
}
} else {
lblMessage.text = "No name entered"
}
} else {
lblMessage.text = "No age entered and must be a number"
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
