@IBAction func segmentedControl(_ sender: UISegmentedControl) {
let number1 = Int(label1.text!)!
let number2 = Int(label1.text!)!
let chosenSegment = sender.selectedSegmentIndex
here
v
let selectedSegmentIndex = segmentedControl.selectedSegmentIndex
if selectedSegmentIndex == 0 {
^
this line of code
result = number1 number2
} else if selectedSegmentIndex == 1 {
result = number1 - number2
} else if selectedSegmentIndex == 2 {
result = number1 * number2
} else if selectedSegmentIndex == 3 {
result = number1 / number2}
}
i am really confused with this can you help me this is an assignment for school, we have to make a calculator.
CodePudding user response:
You seems to access twice :
let number2 = Int(label1.text!)!
let chosenSegment = sender.selectedSegmentIndex
And
let selectedSegmentIndex = segmentedControl.selectedSegmentIndex
with 2 different names. Same method ? If not why not using chosenSegment
CodePudding user response:
segmentedControl
is a function, it doesn't have a member.
But it looks like a typo, the solution is pretty easy. Delete the line
let chosenSegment = sender.selectedSegmentIndex
and replace
let selectedSegmentIndex = segmentedControl.selectedSegmentIndex
with
let selectedSegmentIndex = sender.selectedSegmentIndex