title: Get tapped word from UITextView in Swift
body: <p>I know that this problem has been solved in Objective-C, but I haven't seen any solution to it in Swift. I have tried to convert the solution code from <a href="https://stackoverflow.com/questions/19332283/detecting-taps-on-attributed-text-in-a-uitextview-on-ios-7">this</a> post, but I'm getting errors:</p>
  
  <pre><code>func textTapped(recognizer: UITapGestureRecognizer){
  
      var textView: UITextView = recognizer.view as UITextView
      var layoutManager: NSLayoutManager = textView.layoutManager
      var location: CGPoint = recognizer.locationInView(textView)
      location.x -= textView.textContainerInset.left
      location.y -= textView.textContainerInset.top
  
      var charIndex: Int
      charIndex = layoutManager.characterIndexForPoint(location, inTextContainer: textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
  
      if charIndex &lt; textView.textStorage.length {
          // do the stuff
          println(charIndex)
      }
  }
  </code></pre>
  
  <p>I think the problem is in this line (see error <a href="http://pbrd.co/1sglnt5" rel="noreferrer">here</a>):</p>
  
  <pre><code> var textView: UITextView = recognizer.view as UITextView
  </code></pre>
  
  <p>... which I have converted from Objective-C based on this line:</p>
  
  <pre><code> UITextView *textView = (UITextView *)recognizer.view;
  </code></pre>
  
  <p>Finally, I am also in doubt about how this function should be called. As I understand it, the function should be passed to a Selector in viewDidLoad(), like this:</p>
  
  <pre><code> let aSelector: Selector = "textTapped:"   
  
   let tapGesture = UITapGestureRecognizer(target: self, action: aSelector)
   tapGesture.numberOfTapsRequired = 1
   view.addGestureRecognizer(tapGesture)
  </code></pre>
  
  <p>Because I'm getting the before mentioned error, I'm not sure if it would work. But I'm thinking that I would need to pass the parameter in the textTapped function (recognizer) into the Selector as well. However, I've read that you can only pass the function and not any parameters. </p>