How to make extension for multiple classes Swift

前端 未结 2 2048
天涯浪人
天涯浪人 2021-02-05 02:36

I have an extension:

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

I need to make the same one fo

相关标签:
2条回答
  • 2021-02-05 02:43

    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?". You do that by constraining the extension.

    Example with a where clause:

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

    Thanks to @Knight0fDragon for his excellent comment about the where clause.

    0 讨论(0)
  • 2021-02-05 02:47

    Best Method to Extend UILabel & UIImageView Together

    Swift 4.1 / Xcode 9.4

    A much better way to do this in your case would be to just extend UIView. This works because both UILabel and UIImageView both inherit from UIView.


    Extension

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

    Usage of animateHidden(flag: Bool) Extension

    Declaration of label and imageView:

    label = UILabel()
    imageView = UIImageView()
    

    Actual usage of extension

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

    Bonus - Other Classes that many UI Components Conform to

    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:

    1. CVarArg
    2. Equatable
    3. Hashable
    4. NSCoding

    Some of the many UI components include:

    • UILabel: CVarArg, Equatable, Hashable, NSCoding

    • UITextField: CVarArg, Equatable, Hashable, NSCoding

    • UITableViewCell: CVarArg, Equatable, Hashable, NSCoding

    • UITextView: CVarArg, Equatable, Hashable, NSCoding

    • UITableView: CVarArg, Equatable, Hashable, NSCoding

    • UIImage: CVarArg, Equatable, Hashable, NSCoding

    • UIPickerView: CVarArg, Equatable, Hashable, NSCoding

    • UIView: CVarArg, Equatable, Hashable, NSCoding

    • UIImageView: CVarArg, Equatable, Hashable, NSCoding

    • UINavigationBar: CVarArg, Equatable, Hashable, NSCoding

    • UIButton: CVarArg, Equatable, Hashable, NSCoding

    • UIBarButtonItem: CVarArg, Equatable, Hashable, NSCoding

    • UIStackView: CVarArg, Equatable, Hashable, NSCoding

    • UIToolbar: CVarArg, Equatable, Hashable, NSCoding

    • UITabBar: CVarArg, Equatable, Hashable, NSCoding

    • UITabBarItem: CVarArg, Equatable, Hashable, NSCoding

    • UIScrollView: CVarArg, Equatable, Hashable, NSCoding

    • UISplitViewController: CVarArg, Equatable, Hashable, NSCoding

    • UIViewController: CVarArg, Equatable, Hashable, NSCoding

    • UIScreen: CVarArg

    • UISwitch: CVarArg, Equatable, Hashable, NSCoding

    • UISlider: CVarArg, Equatable, Hashable, NSCoding

    • UIAlertAction: CVarArg

    • UIAlertController: CVarArg, Equatable, Hashable, NSCoding

    • UIImageAsset: CVarArg, Equatable, Hashable, NSCoding

    • UIDatePicker: CVarArg, Equatable, Hashable, NSCoding

    • UINib: CVarArg

    • UIResponder: CVarArg

    • UIWindow: CVarArg, Equatable, Hashable, NSCoding

    • UIRegion: CVarArg, Equatable, Hashable, NSCoding

    • UIControl: CVarArg, Equatable, Hashable, NSCoding

    • UIBezierPath: CVarArg, Equatable, Hashable, NSCoding

    • UIVisualEffect: CVarArg, Equatable, Hashable, NSCoding

    • UISearchBar: CVarArg, Equatable, Hashable, NSCoding

    • UIMenuItem: CVarArg

    • UIMenuController: CVarArg

    • UIStoryboard: CVarArg

    • And many more...


    This means that by extending either CVarArg, Equatable, Hashable, or NSCoding, you can extend most (if every not every) UI component.



    Well anyways, I hope this all helps you resolve your issue and if you have absolutely any questions, suggestions, etc., feel free to ask!



    0 讨论(0)
提交回复
热议问题