RK1979
RK1979

Reputation: 253

Can I use swift Generic in this case

I want to add stored properties to UIView subclass'es like UIView, UIImageView, UIPickerView etc,

I need to create UIView's Instances from the subclass'es only The subclass'es only differ in Type, all the properties and methods are same. The Type's are also conformed on a couple of protocols.

class View: UIView, UIGestureRecognizerDelegate, ProtocolDelegate2 {
    var property1 = CGRectZero
    var property2:Int = 0
    var property3 = false

    func configView(config: [String:AnyObject]) {
        let recognizer = UIPanGestureRecognizer(target: self , action: Selector("panGestureHandler:"))

            recognizer.delegate = self
            addGestureRecognizer(recognizer)
    }

    func panGestureHandler(gestureRecognizer:UIPanGestureRecognizer)
    {

    }
}

class ImageView: UIImageView, UIGestureRecognizerDelegate, ProtocolDelegate2 {
    var property1 = CGRectZero
    var property2:Int = 0
    var property3 = false

    func configView(config: [String:AnyObject]) {
        let recognizer = UIPanGestureRecognizer(target: self , action: Selector("panGestureHandler:"))

            recognizer.delegate = self
            addGestureRecognizer(recognizer)
    }

    func panGestureHandler(gestureRecognizer:UIPanGestureRecognizer)
    {

    }
}

class PickerView: UIPickerView, UIGestureRecognizerDelegate, ProtocolDelegate2 {
    var property1 = CGRectZero
    var property2:Int = 0
    var property3 = false

    func configView(config: [String:AnyObject]) {
        let recognizer = UIPanGestureRecognizer(target: self , action: Selector("panGestureHandler:"))

            recognizer.delegate = self
            addGestureRecognizer(recognizer)
    }

    func panGestureHandler(gestureRecognizer:UIPanGestureRecognizer)
    {

    }
}

The Usage:

let view = View(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let imageView = ImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let pickerView = PickerView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

view.configView(config: JsonData)
view.alpha = 0.5

imageView.configView(config: JsonData)
imageView.alpha = 0.2

pickerView.configView(config: JsonData)
pickerView.backgroundColor = UIColor.orangeColor()

Now the question, is can we use Swift Generics in this case, as the classes only differ in Types. Or is there any better design pattern for better code maintenance.

Upvotes: 1

Views: 558

Answers (1)

fred02138
fred02138

Reputation: 3361

Generics won't help you if your class has to be a subclass of UIView. If you try to do this:

class A<T: UIView> : T {  // ERROR
    var prop: String?
}

the compiler complains that T must be a type or protocol.

You could do this:

class A<T: UIView> {
    var theView: T
    var prop: String?
}

But that's not really better than simply using inheritance:

class A {
    var theView: UIView!
    var prop: String?

    init(view: UIView) {
        theView = view
    }
}

Upvotes: 1

Related Questions