如何为多个类Swift进行扩展

时间:2016-07-19 16:40:44

标签: ios swift swift-extensions

我有一个扩展名:

extension UILabel {
    func animateHidden(flag: Bool) {
        self.hidden = flag
    }
}

我需要为UIImageView制作同一个,但我不想复制整个代码。是否可以为多个类进行扩展?

感谢。

2 个答案:

答案 0 :(得分:49)

您可以制定协议并对其进行扩展。

类似的东西:

protocol Animations {
    func animateHidden(flag: Bool)
}

extension Animations {
    func animateHidden(flag: Bool) {
        // some code
    }
}

extension UILabel: Animations {}

extension UIImageView: Animations {}

您的方法可用于扩展类:

let l = UILabel()
l.animateHidden(false)

let i = UIImageView()
i.animateHidden(false)

在评论中,您曾问过:“在这种情况下,如何在self功能中为UILabelUIImageView拨打animateHidden?”。你可以通过限制扩展来实现。

带有where子句的示例:

extension Animations where Self: UIView {
    func animateHidden(flag: Bool) {
        self.hidden = flag
    }
}

感谢@ Knight0fDragon对where条款的出色评论。

答案 1 :(得分:4)

扩展UILabel&的最佳方法UIImageView一起

Swift 4.1 / Xcode 9.4

在您的情况下,更好的方法是扩展UIView。这是有效的,因为UILabelUIImageView都继承自UIView

扩展

extension UIView {
    func animateHidden(flag: Bool) {
        self.hidden = flag
    }
}

animateHidden(flag: Bool)扩展

的用法

标签声明和imageView

label = UILabel()
imageView = UIImageView()

扩展程序的实际用法

label.animateHidden(flag: true)
imageView.animateHidden(flag: false)

奖励 - 许多UI组件符合的其他类

如果您希望扩展程序能够符合许多不同类型的UI组件,则有4种类型的UI组件符合这些类型:

  1. CVarArg
  2. Equatable
  3. Hashable
  4. NSCoding
  5. 许多UI组件中的一些包括:

    • UILabel: CVarArgEquatableHashableNSCoding

    • UITextField: CVarArgEquatableHashableNSCoding

    • UITableViewCell: CVarArgEquatableHashableNSCoding

    • UITextView: CVarArgEquatableHashableNSCoding

    • UITableView: CVarArgEquatableHashableNSCoding

    • UIImage: CVarArgEquatableHashableNSCoding

    • UIPickerView: CVarArgEquatableHashableNSCoding

    • UIView: CVarArgEquatableHashableNSCoding

    • UIImageView: CVarArgEquatableHashableNSCoding

    • UINavigationBar: CVarArgEquatableHashableNSCoding

    • UIButton: CVarArgEquatableHashableNSCoding

    • UIBarButtonItem: CVarArgEquatableHashableNSCoding

    • UIStackView: CVarArgEquatableHashableNSCoding

    • UIToolbar: CVarArgEquatableHashableNSCoding

    • UITabBar: CVarArgEquatableHashableNSCoding

    • UITabBarItem: CVarArgEquatableHashableNSCoding

    • UIScrollView: CVarArgEquatableHashableNSCoding

    • UISplitViewController: CVarArgEquatableHashableNSCoding

    • UIViewController: CVarArgEquatableHashableNSCoding

    • UIScreen: CVarArg

    • UISwitch: CVarArgEquatableHashableNSCoding

    • UISlider: CVarArgEquatableHashableNSCoding

    • UIAlertAction: CVarArg

    • UIAlertController: CVarArgEquatableHashableNSCoding

    • UIImageAsset: CVarArgEquatableHashableNSCoding

    • UIDatePicker: CVarArgEquatableHashableNSCoding

    • UINib: CVarArg

    • UIResponder: CVarArg

    • UIWindow: CVarArgEquatableHashableNSCoding

    • UIRegion: CVarArgEquatableHashableNSCoding

    • UIControl: CVarArgEquatableHashableNSCoding

    • UIBezierPath: CVarArgEquatableHashableNSCoding

    • UIVisualEffect: CVarArgEquatableHashableNSCoding

    • UISearchBar: CVarArgEquatableHashableNSCoding

    • UIMenuItem: CVarArg

    • UIMenuController: CVarArg

    • UIStoryboard: CVarArg

    • 还有更多......


    这意味着,通过扩展CVarArgEquatableHashableNSCoding,您可以扩展大部分(如果不是每个)UI组件。


    好吧无论如何,我希望这一切都可以帮助您解决问题,如果您有任何问题,建议等,请随时提出!