从UILabel暗中强制转换Swift编译器警告表达式?去任何

时间:2017-02-04 17:50:10

标签: swift swift3 optional

我收到以下Swift编译器警告: Expression implicitly coerced from 'UILabel?' to Any

此外,我发现了以下消息:

<unknown>:0: warning: expression implicitly coerced from 'UILabel?' to Any
<unknown>:0: note: provide a default value to avoid this warning
<unknown>:0: note: force-unwrap the value to avoid this warning
<unknown>:0: note: explicitly cast to Any with 'as Any' to silence this warning

但我不确定为什么。 这是我使用UILabel

的代码
class MyCell: UITableViewCell {
    var primaryLabel: UILabel!
    var secondaryLabel: UILabel!

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:)")
    }

    init(_ row: myStruct) {
        super.init(style: .default, reuseIdentifier: "myId")
        primaryLabel = {
            let l = UILabel(frame: .zero)
            ...
            l.translatesAutoresizingMaskIntoConstraints = false
            return l
        }()
        self.addSubview(primaryLabel)

        if row.subtitle != nil {
            secondaryLabel = {
                let l = UILabel(frame: .zero)
                ...
                l.translatesAutoresizingMaskIntoConstraints = false
                return l
            }()
            self.addSubview(secondaryLabel)
        }

        ... 
    }
}

我做错了什么?或者我如何安全地使编译器静音?

由于

0 个答案:

没有答案