import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var str = "You tapped me"
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
}
}
extension UIViewController: UITableViewDelegate {
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(str)
}
}
CodePudding user response:
I agree with @vadian. Make the following code updates and this should resolve the issue unless I am missing something else.
Current Code:
extension UIViewController: UITableViewDelegate {
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(str)
}
}
Updated Code:
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
print(str)
}
Second Example: Here is an additional example on using extension on a class:
//Declaration on Lamborghini Class:
class Lamborghini: UIImage {
var didTapButton = "Congrats button tapped!"
}
//Extension on Lamborghini Class:
extension Lamborghini: UITableViewDelegate {
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
print(didTapButton)
}
}
Notice that when declaring an extension the Name of the class extended is what follows the extension keyword. Then to the right of the colon the type of class, protocol etc is added.
extension ClassNameHere: SuperClassHere, ProtocolHere { }
Check out the swift documentation on extensions here at Swift Extensions
Hope this helps resolve the issue.
