 import UIKit
 
 protocol TableViewPopoverPresentingHelper: class {
-    func viewControllerForPopoverAtPoint(point: CGPoint) -> UIViewController?
-    func permittedArrowDirectionsForPopoverAtPoint(point: CGPoint) -> UIPopoverArrowDirection
+    func viewControllerForPopoverAtPoint(_ point: CGPoint) -> UIViewController?
+    func permittedArrowDirectionsForPopoverAtPoint(_ point: CGPoint) -> UIPopoverArrowDirection
 }
 
 public protocol TableViewPopoverPresenting: class, UIGestureRecognizerDelegate {


        tableView.addGestureRecognizer(tapGestureRecognizer)
    }
}
     - copyright: ©2016 Lionheart Software LLC
     - date: June 19, 2016
     */
    func viewControllerForPopoverAtIndexPath(_ indexPath: IndexPath) -> UIViewController?
    func permittedArrowDirectionsForPopoverAtIndexPath(_ indexPath: IndexPath) -> UIPopoverArrowDirection?
}

extension UIViewController: TableViewPopoverPresentingHelper {
    func viewControllerForPopoverAtPoint(_ point: CGPoint) -> UIViewController? {
        guard let controller = self as? TableViewPopoverPresenting,
            let indexPath = controller.tableView.indexPathForRow(at: point) else {
            return nil
        }

        return controller.viewControllerForPopoverAtIndexPath(indexPath)
    }

    func permittedArrowDirectionsForPopoverAtPoint(_ point: CGPoint) -> UIPopoverArrowDirection {
        guard let controller = self as? TableViewPopoverPresenting,
            let indexPath = controller.tableView.indexPathForRow(at: point),
            let arrowDirections = controller.permittedArrowDirectionsForPopoverAtIndexPath(indexPath) else {
                return .any
        }

        return arrowDirections ?? .any
    }

    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
        guard let controller = self as? TableViewPopoverPresenting else {
            return true
        }

        if gestureRecognizer is UITapGestureRecognizer {
            let point = touch.location(in: controller.tableView)
            return viewControllerForPopoverAtPoint(point) != nil
        }

        return false
    }

    @objc func handlePopoverGesture(_ gesture: UIGestureRecognizer?) {
        guard let popoverPresenter = self as? TableViewPopoverPresenting,
            let gesture = gesture else {
            return
        }

        let point = gesture.location(in: popoverPresenter.tableView)
        guard let controller = viewControllerForPopoverAtPoint(point) else {
            return
        }

        controller.modalPresentationStyle = .popover

        if let popover = controller.popoverPresentationController {
            popover.sourceView = popoverPresenter.tableView
