0

I have a UIView subclass which contains only the following:

drawRect(rect: CGRect) {
    let ctx: CGContext? = UIGraphicsGetCurrentContext()
    let radius: CGFloat = rect.width / 2
    let endAngle = CGFloat(2 * M_PI)
    CGContextAddArc(ctx, bounds.width / 2, bounds.height / 2, radius, 0, endAngle, 1)
    CGContextSetStrokeColorWithColor(ctx, UIColor.blackColor().CGColor)
    CGContextSetFillColorWithColor(ctx, UIColor.whiteColor().CGColor)
    CGContextSetLineWidth(ctx, 4.0)
    CGContextDrawPath(ctx, CGPathDrawingMode.FillStroke)
}

I do not call any init() functions, but the object is still drawn.

Additionally, when I attempt to implement an init() function, I am given several errors regarding required initializations.

So my question is : How am I able to create a UIView object without an init() function?

1 Answer 1

2

Initializers are inherited. Your superclass (UIView) has init already defined so your class inherits them by default.

Once you define init, your class won't inherit superclass initializers any more so you will have to define all the required ones.

Sign up to request clarification or add additional context in comments.

3 Comments

Just to clarify : If I define what is required, and call super.init() I can override the init() function?
@BrandonBradley Yes, you can. However, for UIView is more common to override init(frame:) which is the designated initializer.
Alright, thanks for the information. Still fairly new to this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.