Error with switch in swift

-1

Well, I'm having an error in the following code. The error is in 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
    }
}
    
asked by anonymous 28.07.2015 / 15:45

1 answer

0

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

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"
        }
    }
}
    
28.07.2015 / 16:33