Reputation: 4168
I am porting old code to Swift. I wrote a custom input view, which works with certain custom views of mine. Here's the convenience method I use to attach the input view:
+ (POGramophoneView *)attachToView:(UIView *)view delegate:(id)delegate
{
if ([view.inputView isKindOfClass:self]) return (POGramophoneView *)view.inputView;
POGramophoneView *gramophone = [[POGramophoneView alloc] initWithFrame:CGRectMake(0, 0, 1024, 267)];
gramophone.attachedView = view;
NSAssert([view respondsToSelector:@selector(setInputView:)], @"Gramophone view %@ cannot attach to %@ because it does not respond to setInputView:", gramophone, view);
// This line
[view performSelector:@selector(setInputView:) withObject:gramophone];
[view reloadInputViews];
return gramophone;
}
Since inputView
of UIView
is readonly, I have to check if the view passed to me can have an input view set. I don't know how I should port the marked line to Swift. How do I check if the passed view has inputView
declared as settable?
class func attachToView(view: UIView, delegate: POGramophoneViewDelegate) -> POGramophoneView {
if view is POGramophoneView {
return view
}
var gramophone = POGramophoneView(frame: CGRectMake(0, 0, 1024, 267))
gramophone.attachedView = view
// set inputview to new gramophone
view.reloadInputViews()
return gramophone
}
Upvotes: 1
Views: 1350
Reputation: 2417
Swift does provide function to convert a string literal into Selector data type. You can write it like this
if view.respondsToSelector(Selector("setInputView:")) {
// To call the selector use KVO technique.
view.setValue(gramophone, forKey:"inputView");
}
Upvotes: 1