Swift Using If Else

One of the most important features in a programming language is decision -making structures. Thanks to these structures, we can decide where the result will reach in our application. The if-Else structure, which is almost the same in programming languages such as C# and Java, is slightly different from the others in Swift. After writing if I write in C# and Java, there is no condition such as opening parentheses in Swift programming language while writing the condition in parentheses.

Let’s apply simple lice to give an example. In this application, let’s ask the user to enter an article. If this article is how are you writing, I’m fine, I don’t understand the screen, and in others. In addition, this program is done on the keyboard as soon as I say these operations.

In terms of design, let’s add one textfield, adding label under Textfield and respond to the textfield. The purpose here was a simple design here already understands the ifse structure.

After taking care of the design section, we need to buy Textfield and Label as an outlet. After making these definitions, we need to create a function in Textfield. This function will allow us to define what will happen after our writing process at TextFiled. First, we will create a function by pressing the right button in Textfield. The point we need to pay attention to here is that we need to select the Did on Exit type in the type section. The reason we select this is for this function to be activated immediately after our work is finished. Then let’s go back to our story. If you write how you are in my writing field, I’m fine in Label, I would write in other words I did not understand label. For this, we need to create a control in the function. In this control, he will say that I am fine if he says how you are, and in other cases I would not understand.

@IBOutlet weak var girilecekYazi: UITextField!
    @IBOutlet weak var cevapKismi: UILabel!
    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.

    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated.

    @IBAction func buttonCevapla(_ sender: Any) {
        if girilecekYazi.text=="nasılsın"
            cevapKismi.text = "İyiyim"
        else {
            cevapKismi.text = "anlamadım"

The answer we get when we write I’m fine on the screen;

When we type something different in the text field, they will appear on the screen.


Leave a Reply

Your email address will not be published. Required fields are marked *

Press ESC to close