0

I made a segmented control in swift that changes a boolean to either true or false; However, every time I select "selectedSegmentedIndex == 1" in the application, I get error "Thread 1: signal SIGABERT"

My code goes as flows:

@IBOutlet weak var translationType: UISegmentedControl!

var state = true

@IBAction func translation(_ sender: Any)
{
    if translationType.selectedSegmentIndex == 0
    {
     state = ture
    }
    else if translationType.selectedSegmentIndex == 1
    {
     state = false
    }
}

Any information would be greatly appreciated. Thanks.

2
  • My guess is that you didn't link correctly this line @IBOutlet weak var translationType: UISegmentedControl! with the corresponding item in the Storyboard. Commented Aug 5, 2018 at 19:55
  • yep, sounds about right, because the code works perfectly Commented Aug 5, 2018 at 22:00

3 Answers 3

1

At least using the sender parameter and the static type avoids the crash if translationType is not connected – which is most likely the case.

@IBAction func translation(_ sender: UISegmentedControl)
{
    if sender.selectedSegmentIndex == 0
    {
     state = true
    }
    else if sender.selectedSegmentIndex == 1
    {
     state = false
    }
}

or a bit shorter

@IBAction func translation(_ sender: UISegmentedControl)
{
     state = sender.selectedSegmentIndex == 0
}
Sign up to request clarification or add additional context in comments.

Comments

0
var state = true

@IBOutlet weak var translationType: UISegmentedControl!

@IBAction func translation(_ sender: UISegmentedControl)
{
    if translationType.selectedSegmentIndex == 0
    {
        state = true
    }
    else if translationType.selectedSegmentIndex == 1
    {
        state = false
    }

    print(state)
    print(translationType.selectedSegmentIndex)
}

4 Comments

This crashes as well if translationType is nil
well if the translationType is nil than uts not properly connected myfriend , reconnect it from the storyboard
Yes, therefore your code makes no difference at all.
just trying to help my dear friend
0

make sure your outlet is connected! see connection inspector

1 Comment

This could have been a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.