swift 属性 未在 super.init 调用时初始化
swift Property not initialized at super.init call
我有一个名为 badge 的 class 继承自另一个 class,我希望构造函数方法告诉通知的样式。
但我遇到了这个错误:
属性 'self.notificationStyle' 未在 super.init 调用时初始化
DefaultTableViewCell class
final public class DefaultTableViewCell: UITableViewCell
enum NotificationStyle {
case numberedSquare, circle
}
var notificationStyle: NotificationStyle = .numberedSquare
我的目标是每当有人实例化这个 Badge class 时,有必要将它通知 notificationStyle,在这种情况下是方形或圆形。
我该如何解决这个问题?
Badge class
@objc public class Badge: NotifyLabel
var notificationStyle: DefaultTableViewCell.NotificationStyle
init(frame: CGRect, notificationStyle: DefaultTableViewCell.NotificationStyle) {
self.notificationStyle = notificationStyle
super.init(frame: frame)
setup(notificationStyle: notificationStyle)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup(notificationStyle: notificationStyle)
}
init(notificationStyle: DefaultTableViewCell.NotificationStyle) {
self.notificationStyle = notificationStyle
super.init(frame: .zero)
setup(notificationStyle: notificationStyle)
}
func configureBadgeNotificationStyle(notificationStyle: DefaultTableViewCell.NotificationStyle) {
textAlignment = NSTextAlignment.center
layer.borderWidth = 1
layer.borderColor = Color.bostonRed.cgColor
clipsToBounds = true
textStyle = .label
backgroundColor = Color.white
switch notificationStyle {
case .circle:
layer.cornerRadius = 8
default:
layer.cornerRadius = 2
}
}
private func setup(notificationStyle: DefaultTableViewCell.NotificationStyle) {
configureBadgeNotificationStyle(notificationStyle: notificationStyle)
configureAccessibility()
}
NotifyLabel class
public class NotifyLabel: UILabel
public init(textStyle: TextStyle) {
self.textStyle = textStyle
super.init(frame: .zero)
applyTextStyle()
}
public override init(frame: CGRect) {
super.init(frame: frame)
applyTextStyle()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
applyTextStyle()
}
您可以通过添加一行将 notificationStyle
设置为 init?(coder aDecoder: NSCoder)
中的默认值来解决此问题:
required init?(coder aDecoder: NSCoder) {
self.notificationStyle = .numberedSquare //<-- Here
super.init(coder: aDecoder)
setup(notificationStyle: notificationStyle)
}
你必须这样做,因为在你的 notificationStyle
声明中,没有默认值,它必须在调用 super.init
之前有一个值。在你的其他初始化器中,你根据传入的参数设置它。
这是一个初始值设定项,听起来您并没有使用它,但是 UIView
需要我们实现这个必需的初始值设定项。
我有一个名为 badge 的 class 继承自另一个 class,我希望构造函数方法告诉通知的样式。
但我遇到了这个错误: 属性 'self.notificationStyle' 未在 super.init 调用时初始化
DefaultTableViewCell class
final public class DefaultTableViewCell: UITableViewCell
enum NotificationStyle {
case numberedSquare, circle
}
var notificationStyle: NotificationStyle = .numberedSquare
我的目标是每当有人实例化这个 Badge class 时,有必要将它通知 notificationStyle,在这种情况下是方形或圆形。
我该如何解决这个问题?
Badge class
@objc public class Badge: NotifyLabel
var notificationStyle: DefaultTableViewCell.NotificationStyle
init(frame: CGRect, notificationStyle: DefaultTableViewCell.NotificationStyle) {
self.notificationStyle = notificationStyle
super.init(frame: frame)
setup(notificationStyle: notificationStyle)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup(notificationStyle: notificationStyle)
}
init(notificationStyle: DefaultTableViewCell.NotificationStyle) {
self.notificationStyle = notificationStyle
super.init(frame: .zero)
setup(notificationStyle: notificationStyle)
}
func configureBadgeNotificationStyle(notificationStyle: DefaultTableViewCell.NotificationStyle) {
textAlignment = NSTextAlignment.center
layer.borderWidth = 1
layer.borderColor = Color.bostonRed.cgColor
clipsToBounds = true
textStyle = .label
backgroundColor = Color.white
switch notificationStyle {
case .circle:
layer.cornerRadius = 8
default:
layer.cornerRadius = 2
}
}
private func setup(notificationStyle: DefaultTableViewCell.NotificationStyle) {
configureBadgeNotificationStyle(notificationStyle: notificationStyle)
configureAccessibility()
}
NotifyLabel class
public class NotifyLabel: UILabel
public init(textStyle: TextStyle) {
self.textStyle = textStyle
super.init(frame: .zero)
applyTextStyle()
}
public override init(frame: CGRect) {
super.init(frame: frame)
applyTextStyle()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
applyTextStyle()
}
您可以通过添加一行将 notificationStyle
设置为 init?(coder aDecoder: NSCoder)
中的默认值来解决此问题:
required init?(coder aDecoder: NSCoder) {
self.notificationStyle = .numberedSquare //<-- Here
super.init(coder: aDecoder)
setup(notificationStyle: notificationStyle)
}
你必须这样做,因为在你的 notificationStyle
声明中,没有默认值,它必须在调用 super.init
之前有一个值。在你的其他初始化器中,你根据传入的参数设置它。
这是一个初始值设定项,听起来您并没有使用它,但是 UIView
需要我们实现这个必需的初始值设定项。