swift 中的 UIView 初始方法总是显示错误
UIView initial method in swift always shows error
我有一个从 UIView
派生的自定义视图,在 init
方法中是这样的:
- (instancetype)init
{
if ([super init])
{
self.frame = [UIScreen mainScreen].bounds;
or self = [[UIView alloc]init];
}
return self;
}
它从不显示错误,但是当我使用 Swift
init() {
self.frame = UIScreen.mainScreen().bounds
}
编译器告诉我:
Super.init
isn't called on all paths before returning from initialiser
然后我添加 super.init()
,编译器告诉我:
Convenience initializer is declared here (UIKit.UIView
)
如何在 Swift init
方法中使用 self = [[UIView alloc]init];
等初始方法?
试试这个...
init()
{
super.init(frame: UIScreen.mainScreen().bounds)
// Other initialization operations...
}
我有一个从 UIView
派生的自定义视图,在 init
方法中是这样的:
- (instancetype)init
{
if ([super init])
{
self.frame = [UIScreen mainScreen].bounds;
or self = [[UIView alloc]init];
}
return self;
}
它从不显示错误,但是当我使用 Swift
init() {
self.frame = UIScreen.mainScreen().bounds
}
编译器告诉我:
Super.init
isn't called on all paths before returning from initialiser
然后我添加 super.init()
,编译器告诉我:
Convenience initializer is declared here (
UIKit.UIView
)
如何在 Swift init
方法中使用 self = [[UIView alloc]init];
等初始方法?
试试这个...
init()
{
super.init(frame: UIScreen.mainScreen().bounds)
// Other initialization operations...
}