Error with switch in Swift

Asked

Viewed 48 times

-1

Guys, I’m having an error in the following code. The error is on the switch, the following error appears:

expected declaration.

What to do?

import UIKit

class ViewControllerAnalseOP1: UIViewController, UITextViewDelegate {
    var indice: Int?
    var string1 = "Primeira String"
    var string2 = "Segunda String"

    @IBOutlet weak var descricao: UITextView!

    switch indice {
        case 0:
            descricao.text = string1
        case 1:
            descricao.text = string2
        default:
            descricao.text = "Not Found"
    }

    func textViewShouldBeginEditing(descricao: UITextView) -> Bool {
        return false
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        descricao.delegate = self
    }
}
  • I saw it written so wrong

1 answer

0


The syntax error appears because the switch is declared in the class body. The switch must be declared within some method, e.g.:

class ViewControllerAnalseOP1: UIViewController, UITextViewDelegate {
    var indice: Int = 0
    var string1 = "Primeira String"
    var string2 = "Segunda String"

    @IBOutlet weak var descricao: UITextView!

    func textViewShouldBeginEditing(descricao: UITextView) -> Bool {
        return false
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        descricao.delegate = self

        switch indice {
            case 0:
                descricao.text = string1
            case 1:
                descricao.text = string2
            default:
                descricao.text = "Not Found"
        }
    }
}
  • Thank you very much it worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.