Search code examples
iosiphoneswiftuikitinit

UIView initial method in swift always shows error


I have a custom view derived from UIView,in init method like this:

- (instancetype)init
{
    if ([super init])
    {
        self.frame = [UIScreen mainScreen].bounds;
or self = [[UIView alloc]init];
    }    
    return self;
}

It never shows the error, but when I use Swift

init() {
    self.frame = UIScreen.mainScreen().bounds
}

The compiler tells me:

Super.init isn't called on all paths before returning from initialiser

then I add super.init() and the compiler tells me:

Convenience initializer is declared here (UIKit.UIView)

How can I use some initial method like self = [[UIView alloc]init]; in a Swift init method?


Solution

  • Try with this...

    init()
    {
        super.init(frame: UIScreen.mainScreen().bounds)
    
        // Other initialization operations...
    }