rb2030 rb2030 - 17 days ago 6
Swift Question

Using SnapKit, finding it hard to append new constraints to existing constraints

I am currently using SnapKit with Swift, using a walkthrough to make a quiz app. I am currently making some constraints, and would like to append some new constraints to my array of constraints

I am getting the error "Value of Tuple Type '()' has no member answerButtonsConstraints".

But I don't understand why. Could someone please help.

I have tried remaking the constraints using SnapKits remakeConstraints, but I did not get the result I was hoping for. I am hoping that I can create the correct outcome with a simple syntax change but am unsure of how to format it. Any help would be very much appreciated.

var answersButtonsConstraints = [

self.answerButtons[0].snp.makeConstraints { (make) in
make.leading.equalTo(answerView)
make.trailing.equalTo(answerButtons[1].snp.leading).offset(MultipleChoiceViewController.buttonPadding)
make.top.equalTo(answerView.snp.top)
make.bottom.equalTo(answerButtons[2].snp.top).offset(MultipleChoiceViewController.buttonPadding)
},

self.answerButtons[1].snp.makeConstraints { (make) in
make.trailing.equalTo(answerView)
make.top.equalTo(answerView)
make.bottom.equalTo(answerButtons[3]).offset(MultipleChoiceViewController.buttonPadding)
},

self.answerButtons[2].snp.makeConstraints { (make) in
make.leading.equalTo(answerView)
make.trailing.equalTo(answerButtons[3].snp.leading).offset(MultipleChoiceViewController.buttonPadding)
make.bottom.equalTo(answerView)
},

self.answerButtons[3].snp.makeConstraints { (make) in
make.trailing.equalTo(answerView)
make.bottom.equalTo(answerView)
}]

for index in 1..<answerButtons.count {

answersButtonsConstraints.append().self.answerButtons[index].snp.makeConstraints { (make) in
make.height.equalTo(answerButtons[index-1].snp.height)
make.width.equalTo(answerButtons[index-1].snp.width)
}
}
}


At the bottom there, where it says answersButtonsConstrains.append().self.answerButtons.... is where I am receiving my error.

Answer Source

You have syntax error there: you need to pass argument to append method inside brackets, not after .

    for index in 1..<answerButtons.count {
        answersButtonsConstraints.append(self.answerButtons[index].snp.makeConstraints { (make) in
            make.height.equalTo(answerButtons[index-1].snp.height)
            make.width.equalTo(answerButtons[index-1].snp.width)
        }
    })

Or if you want to make it a bit more readable make a local variable for constraint:

for index in 1..<answerButtons.count {
        let constraint = self.answerButtons[index].snp.makeConstraints { (make) in
        make.height.equalTo(answerButtons[index-1].snp.height)
        make.width.equalTo(answerButtons[index-1].snp.width)
    }
    answersButtonsConstraints.append(constraint)
}