【问题标题】:swift Property not initialized at super.init callswift 属性未在 super.init 调用中初始化
【发布时间】:2021-12-22 13:57:54
【问题描述】:

我有一个名为 badge 的类,它继承自另一个类,我希望构造函数方法告诉通知的样式。

但我遇到了这个错误: 属性“self.notificationStyle”未在 super.init 调用时初始化

DefaultTableViewCell 类

final public class DefaultTableViewCell: UITableViewCell

enum NotificationStyle {
        case numberedSquare, circle
    }
    
    var notificationStyle: NotificationStyle = .numberedSquare

我的目标是每当有人实例化这个 Badge 类时,都需要通知它的 notificationStyle,在这种情况下是方形或圆形。

我该如何解决这个问题?

徽章等级

@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 类

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()
    }

【问题讨论】:

    标签: ios swift oop inheritance init


    【解决方案1】:

    您可以通过添加一行将notificationStyle 设置为init?(coder aDecoder: NSCoder) 中的默认值来解决此问题:

    required init?(coder aDecoder: NSCoder) {
        self.notificationStyle = .numberedSquare //<-- Here
        super.init(coder: aDecoder)
        setup(notificationStyle: notificationStyle)
    }
    

    您必须这样做,因为在您的notificationStyle 声明中,没有默认值,并且在调用super.init 之前它必须有一个值。在您的其他初始化程序中,您根据传入的参数设置它。

    这是一个初始化器,听起来您好像并没有使用,但UIViews 要求我们实现这个必需的初始化器。

    【讨论】:

    • 但是如果我像这样实例化我的徽章类 Badge(notificationStyle: .circle) 我的徽章要成正方形,我需要我的徽章是圆形或正方形,基于在他的 init 方法中传递的 notificationStyle
    • 不,不会。这完全是一个不同的初始化程序。你已经定义了它,代码看起来很好。错误来自您未使用的初始化程序。您是否尝试过解决方案?
    • 我忘记了一个步骤,对不起,它工作正常!!谢谢! @jnpdx
    猜你喜欢
    • 2020-07-06
    • 2014-10-09
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 2014-07-24
    相关资源
    最近更新 更多