The 3rd example of writing the if statement causes all rows == 0 to not resignFirstResponder() regardless of their section.
hideKeyboard() method, first if statement example (pg 598) :: Works
@objc func hideKeyboard(_ gestureRecognizer: UIGestureRecognizer) {
let point = gestureRecognizer.location(in: tableView)
let indexPath = tableView.indexPathForRow(at: point)
if indexPath != nil && indexPath!.section == 0
&& indexPath!.row == 0 {
return
}
descriptionTextView.resignFirstResponder()
}
If statement alternative way 2 (pg 599) :: Works
if indexPath == nil ||
!(indexPath!.section == 0 && indexPath!.row == 0) {
descriptionTextView.resignFirstResponder()
}
If statement alternative way 3 (pg 599) :: Broken
if let indexPath = indexPath, indexPath.section != 0 &&
indexPath.row != 0 {
descriptionTextView.resignFirstResponder()
}
If statement alternative way 3 (pg 599) :: Fix
Allows for any row == 0, except for that in section 0, to resignFirstResponder()
if let indexPath = indexPath, !(indexPath.section == 0 &&
indexPath.row == 0) {
descriptionTextView.resignFirstResponder()
}
As a Guard let statement :: Works, but I have a question about shadowing indexPath
guard let path = indexPath, !(path.section == 0 &&
path.row == 0) else { return }
descriptionTextView.resignFirstResponder()
I get an error when I try to shadow indexPath?
in assigning it indexPath
via guard let.
Error 'Definition conflicts with previous value'
in the same method
let indexPath = tableView.indexPathForRow(at: point) // conflicting def
I had to assign it to a different constant name, so I chose path
.
Question: Is there a way to write this that would allow me to shadow indexPath?
using guard let? Or would I always have to chose a different constant name in situations like this?
Thanks so much!
Rebecca - i2j3