ios - 我可以在 Swift 中强制协议(protocol)符合吗?

标签 ios objective-c swift protocols

我想这样做:

UIView <UITextFieldDelegate>*

很快。

使子类化 UIView 的对象也符合 UITextFieldDelegate协议(protocol)。

最佳答案

可以表达(id <UITextFieldDelegate, UIScrollViewDelegate>)使用 Protocol Composition
但不是(UIView<UITextFieldDelegate> *) .除了类定义

// Obj-C
- (void)methodName:(id <UITextFieldDelegate, UIScrollViewDelegate>)arg { ... }

// Swift
func methodName(arg:protocol<UITextFieldDelegate, UIScrollViewDelegate>!) { ... }

实际上,一个 Obj-C 方法声明为 - (void)methodName((UIView<UITextFieldDelegate> *))arg; ,当它桥接到 Swift 时,你可以用任何 UIView 调用实例。

enter image description here

编辑:

经过一些研究,您似乎可以声明您的 func像这样

func myFunc<T:UIView where T:UITextFieldDelegate>(view:T) { ... }

关于ios - 我可以在 Swift 中强制协议(protocol)符合吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26009107/

相关文章:

ios - 为什么仅当我的 iPhone 未连接到 Mac 时才会出现警告?

ios - 在 UITableViewCell 子类中实现自定义滑动功能

ios - 更改按钮文本在 iOS 上不起作用

ios - 如何水平定位 UINavigationBar 中的 backIndicatorImage

xcode - Swift - 无法使用 UITableViewCell 类型的参数调用 '!'

ios - 通过 Storyboard以模态方式在选项卡栏中呈现特定选项卡

ios - 如何在 iPhone 稍微滚动时停止 CLLocationManager 标题值更改。

iphone - UIButton 触摸时导致应用程序崩溃

ios - Swift 异步图像加载器

ios - 为什么我无法从 swift5 中的模拟器获取设备 token ?