[英]How to make extension for multiple classes Swift
I have an extension: 我有一个扩展名:
extension UILabel {
func animateHidden(flag: Bool) {
self.hidden = flag
}
}
I need to make the same one for UIImageView
but I don't want to copy that whole code. 我需要为
UIImageView
制作相同的一个,但我不想复制整个代码。 Is it possible to make an extension for multiple classes? 是否可以为多个类进行扩展?
Thanks. 谢谢。
You could make a protocol and extend it. 您可以制定协议并对其进行扩展。
Something like: 就像是:
protocol Animations {
func animateHidden(flag: Bool)
}
extension Animations {
func animateHidden(flag: Bool) {
// some code
}
}
extension UILabel: Animations {}
extension UIImageView: Animations {}
Your method will be available for the extended classes: 您的方法可用于扩展类:
let l = UILabel()
l.animateHidden(false)
let i = UIImageView()
i.animateHidden(false)
In a comment, you've asked: "in this case how to call self
for UILabel
and UIImageView
in animateHidden
function?". 在评论中,您已经问过:“在这种情况下,如何在
animateHidden
函数中为UILabel
和UIImageView
调用self
?”。 You do that by constraining the extension. 您可以通过约束扩展来实现。
Example with a where
clause: 带有
where
子句的示例:
extension Animations where Self: UIView {
func animateHidden(flag: Bool) {
self.hidden = flag
}
}
Thanks to @Knight0fDragon for his excellent comment about the where
clause. 感谢@ Knight0fDragon对于
where
子句的出色评论。
UILabel
& UIImageView
Together UILabel
和UIImageView
最佳方法 A much better way to do this in your case would be to just extend UIView
. 在你的情况下,更好的方法是扩展
UIView
。 This works because both UILabel
and UIImageView
both inherit from UIView
. 这是有效的,因为
UILabel
和UIImageView
都继承自UIView
。
extension UIView {
func animateHidden(flag: Bool) {
self.hidden = flag
}
}
animateHidden(flag: Bool)
Extension animateHidden(flag: Bool)
扩展 Declaration of label and imageView : 标签和图像声明查看:
label = UILabel()
imageView = UIImageView()
Actual usage of extension 扩展的实际用法
label.animateHidden(flag: true)
imageView.animateHidden(flag: false)
If you would like to have your extension be able to conform with many different types of UI components, there are 4 types that a very large amount of UI components conform to: 如果您希望扩展能够符合许多不同类型的UI组件,则有大量UI组件符合的4种类型:
CVarArg
Equatable
Hashable
NSCoding
Some of the many UI components include: 许多UI组件中的一些包括:
UILabel: CVarArg
, Equatable
, Hashable
, NSCoding
UILabel:
CVarArg
, Equatable
, Hashable
, NSCoding
UITextField: CVarArg
, Equatable
, Hashable
, NSCoding
UITextField:
CVarArg
, Equatable
, Hashable
, NSCoding
UITableViewCell: CVarArg
, Equatable
, Hashable
, NSCoding
UITableViewCell:
CVarArg
, Equatable
, Hashable
, NSCoding
UITextView: CVarArg
, Equatable
, Hashable
, NSCoding
UITextView:
CVarArg
, Equatable
, Hashable
, NSCoding
UITableView: CVarArg
, Equatable
, Hashable
, NSCoding
UITableView:
CVarArg
, Equatable
, Hashable
, NSCoding
UIImage: CVarArg
, Equatable
, Hashable
, NSCoding
UIImage:
CVarArg
, Equatable
, Hashable
, NSCoding
UIPickerView: CVarArg
, Equatable
, Hashable
, NSCoding
UIPickerView:
CVarArg
, Equatable
, Hashable
, NSCoding
UIView: CVarArg
, Equatable
, Hashable
, NSCoding
UIView:
CVarArg
, Equatable
, Hashable
, NSCoding
UIImageView: CVarArg
, Equatable
, Hashable
, NSCoding
UIImageView:
CVarArg
, Equatable
, Hashable
, NSCoding
UINavigationBar: CVarArg
, Equatable
, Hashable
, NSCoding
UINavigationBar:
CVarArg
, Equatable
, Hashable
, NSCoding
UIButton: CVarArg
, Equatable
, Hashable
, NSCoding
UIButton:
CVarArg
, Equatable
, Hashable
, NSCoding
UIBarButtonItem: CVarArg
, Equatable
, Hashable
, NSCoding
UIBarButtonItem:
CVarArg
, Equatable
, Hashable
, NSCoding
UIStackView: CVarArg
, Equatable
, Hashable
, NSCoding
UIStackView:
CVarArg
, Equatable
, Hashable
, NSCoding
UIToolbar: CVarArg
, Equatable
, Hashable
, NSCoding
UIToolbar:
CVarArg
, Equatable
, Hashable
, NSCoding
UITabBar: CVarArg
, Equatable
, Hashable
, NSCoding
UITabBar:
CVarArg
, Equatable
, Hashable
, NSCoding
UITabBarItem: CVarArg
, Equatable
, Hashable
, NSCoding
UITabBarItem:
CVarArg
, Equatable
, Hashable
, NSCoding
UIScrollView: CVarArg
, Equatable
, Hashable
, NSCoding
UIScrollView:
CVarArg
, Equatable
, Hashable
, NSCoding
UISplitViewController: CVarArg
, Equatable
, Hashable
, NSCoding
UISplitViewController:
CVarArg
, Equatable
, Hashable
, NSCoding
UIViewController: CVarArg
, Equatable
, Hashable
, NSCoding
UIViewController:
CVarArg
, Equatable
, Hashable
, NSCoding
UIScreen: CVarArg
UIScreen:
CVarArg
UISwitch: CVarArg
, Equatable
, Hashable
, NSCoding
UISwitch:
CVarArg
, Equatable
, Hashable
, NSCoding
UISlider: CVarArg
, Equatable
, Hashable
, NSCoding
UISlider:
CVarArg
, Equatable
, Hashable
, NSCoding
UIAlertAction: CVarArg
UIAlertAction:
CVarArg
UIAlertController: CVarArg
, Equatable
, Hashable
, NSCoding
UIAlertController:
CVarArg
, Equatable
, Hashable
, NSCoding
UIImageAsset: CVarArg
, Equatable
, Hashable
, NSCoding
UIImageAsset:
CVarArg
, Equatable
, Hashable
, NSCoding
UIDatePicker: CVarArg
, Equatable
, Hashable
, NSCoding
UIDatePicker:
CVarArg
, Equatable
, Hashable
, NSCoding
UINib: CVarArg
UINib:
CVarArg
UIResponder: CVarArg
UIResponder:
CVarArg
UIWindow: CVarArg
, Equatable
, Hashable
, NSCoding
UIWindow:
CVarArg
, Equatable
, Hashable
, NSCoding
UIRegion: CVarArg
, Equatable
, Hashable
, NSCoding
UIRegion:
CVarArg
, Equatable
, Hashable
, NSCoding
UIControl: CVarArg
, Equatable
, Hashable
, NSCoding
UIControl:
CVarArg
, Equatable
, Hashable
, NSCoding
UIBezierPath: CVarArg
, Equatable
, Hashable
, NSCoding
UIBezierPath:
CVarArg
, Equatable
, Hashable
, NSCoding
UIVisualEffect: CVarArg
, Equatable
, Hashable
, NSCoding
UIVisualEffect:
CVarArg
, Equatable
, Hashable
, NSCoding
UISearchBar: CVarArg
, Equatable
, Hashable
, NSCoding
UISearchBar:
CVarArg
, Equatable
, Hashable
, NSCoding
UIMenuItem: CVarArg
UIMenuItem:
CVarArg
UIMenuController: CVarArg
UIMenuController:
CVarArg
UIStoryboard: CVarArg
UIStoryboard:
CVarArg
And many more... 还有很多...
CVarArg
, Equatable
, Hashable
, or NSCoding
, you can extend most (if every not every) UI component. CVarArg
, Equatable
, Hashable
或NSCoding
,您可以扩展大多数(如果不是每个)UI组件。
Well anyways, I hope this all helps you resolve your issue and if you have absolutely any questions, suggestions, etc., feel free to ask! 好吧无论如何,我希望这一切都可以帮助您解决问题,如果您有任何问题,建议等,请随时提出!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.