Reputation: 25
Fast search for this question gave me nothing. At the same time it's hard to explain, I guess all is clear from this method. Take a look at this, please:
func tap(sender: AnyObject, action: Selector) -> UITapGestureRecognizer {
let tap = UITapGestureRecognizer(target: sender, action: action)
tap.delegate = sender
return tap
}
Error :
Type 'AnyObject' does not conform to protocol 'UIGestureRecognizerDelegate'
The question is what need to write extra for AnyObject
to avoid of getting this message? Or any other ways?
Upvotes: 0
Views: 63
Reputation: 8164
Assuming, that sender
always is of type UIGestureRecognizerDelegate
you can use a forced downcast:
tap.delegate = sender as UIGestureRecognizerDelegate
Otherwise, use a failing cast:
if let delegate = sender as? UIGestureRecognizerDelegate
{
tap.delegate = delegate
}
As reference, have a look at Type Casting for Any and AnyObject contained in the Swift programming language guide.
Note, that to me this style of assigning the delegate seems to be unusual and error prone.
Upvotes: 1