Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 无法解释'|';性格_Swift_Swift2_Autolayout - Fatal编程技术网

Swift 无法解释'|';性格

Swift 无法解释'|';性格,swift,swift2,autolayout,Swift,Swift2,Autolayout,我需要你的帮助,因为我不明白我的自动约束是怎么回事。我的开关的限制使应用程序崩溃。当我移除它们时,效果很好。 这是我收到的错误消息: 无法解释“|”字符,因为相关视图没有superview H:|-100-[v0(35)]| 谢谢你的帮助 这是我的密码: class selectionCustomCell: UITableViewCell{ var label: UILabel = { let attribution = UILabel() attribu

我需要你的帮助,因为我不明白我的自动约束是怎么回事。我的开关的限制使应用程序崩溃。当我移除它们时,效果很好。 这是我收到的错误消息: 无法解释“|”字符,因为相关视图没有superview H:|-100-[v0(35)]|

谢谢你的帮助

这是我的密码:

class selectionCustomCell: UITableViewCell{
    var label: UILabel = {
        let attribution = UILabel()
        attribution.text = "Nom du label"
        attribution.textColor = UIColor(r: 0, g: 185, b: 255)
        attribution.lineBreakMode = NSLineBreakMode.ByWordWrapping
        attribution.numberOfLines = 0
        attribution.translatesAutoresizingMaskIntoConstraints = false
        return attribution
    }()

  var switchElement: UISwitch{
        let sL = UISwitch()
        sL.setOn(true, animated: true)
        sL.onTintColor = UIColor(r: 0, g: 185, b: 255)
        sL.tintColor = UIColor(r: 0, g: 185, b: 255)
        sL.translatesAutoresizingMaskIntoConstraints = false
        return sL
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Default, reuseIdentifier: reuseIdentifier)
        addSubview(switchElement)
        addSubview(label)
        setupViews()
    }

    func setupViews(){
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": label]))          
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[v0]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": label]))


        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-100-[v0(35)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": switchElement]))


        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[v0(35)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": switchElement]))


    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

注意
label
switchView
的声明方式之间的区别:
label
初始化为闭包的输出,第一次引用闭包时执行闭包
switchView
是一个计算属性,每次引用它时都会调用该属性的getter,这意味着您在
-SetupView
中引用的版本与之前在上一节中调用的
-addSubview
不同。由于它们不属于视图层次结构,因此视觉格式无效

如果您使
开关视图
的声明与
标签
的声明匹配,则您的代码应按预期工作:

var switchElement: UISwitch = { // note the assignment operator here
    let sL = UISwitch()
    // ...
    return sL
}() // note the invocation of the block here