First of all, I've checked all of the other questions on this fatal error, and mine is different from the others.
import Firebase
var tappedUserController = TappedUserController()
class TappedUserController: UIViewController {
@IBOutlet weak var tappedName: UILabel!
@IBOutlet weak var tappedSport: UILabel!
@IBOutlet weak var tappedZip: UILabel!
@IBOutlet weak var tappedPhone: UILabel!
@IBOutlet weak var tappedEmail: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let peopleView = PeopleViewController()
let number = myIndex
print(number)
let tappedRow = peopleView.people[number]
print(tappedRow)
let tappedUser = FirebaseFirestore.root.collection("users").document()
override public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
print(myIndex)
print(people[0])
performSegue(withIdentifier: "toTappedUser", sender: self)
}
0 Person(name: "Test1", zip: "95111", sport: "Tennis", email: "test1@email.com") 0 Fatal error: Index out of range 2019-08-24 18:12:25.041506-0700 Sports Partner[29777:4579375] Fatal error: Index out of range (lldb)
I'm trying to make an app about users and sports. On the right side (or second section of the code), when using didSelectRowAt
, it should have gone to make myIndex
to the indexPath.row
. So if I tapped the first row when running the app, it should let myIndex
equal to 0
.
When printing myIndex
, it also outputs 0
.
Now when I print people[0]
, which should be the same as people[myIndex]
, it outputs what I want it to output, the data for that specific row.
Then, using performSegue
, it goes to the next View Controller with the class on the left side (or the first section of code). I tried to get another variable (number) to equal to myIndex
, but it doesn't change the output.
Printing number
, it outputs 0
again. But when I get the public class peopleView
and the public array people
to do peopleView.people[number]
, the error
Fatal error: Index out of range
occurs.
I am very confused as to why this is happening, because printing people[0]
should be the same as printing people[myIndex]
. Can someone explain why this is happening?
EDIT
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let TappedUserController = segue.destination as? TappedUserController,
let index = tableView.indexPathForSelectedRow?.row
else {
return
}
tappedUserController.selectedPerson = people[myIndex]
}