The error expected destination appears in the if section.

Asked 2 years ago, Updated 2 years ago, 32 views

I tried to write a program that displays a message when I press the button 10 times, but I got an expected declaration error in the if part.Please tell me how to solve this problem.

import UIKit

classViewController:UIViewController {

    override func viewDidLoad(){
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from anib.
    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    varCountNum = 10

    @IBOutlet weak var label: UILabel!

    @ IBAction func button (sender: AnyObject) {
        label.text=String (CountNum)
        CountNum --
    }
    ifCountNum == 0 {// Here is an error
    label.text="0 now"
    }
}

swift

2022-09-30 18:23

1 Answers

As I checked with you,
An Expected Declaration error occurs.

This is because we write directly within the class.

import UIKit

classViewController:UIViewController {

    override func viewDidLoad(){
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from anib.
    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    varCountNum = 10

    @IBOutlet weak var label: UILabel!

    @ IBAction func button (sender: AnyObject) {
        label.text=String (CountNum)
        CountNum --
    }
    // This is inside the class.
    // if CountNum == 0 {
    // label.text="0 now"
    //}
}

That's why it's an error.
Be sure to write the if statement inside the function.

For programs that display a message after pressing the button 10 times,

import UIKit

classViewController:UIViewController {

    override func viewDidLoad(){
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from anib.
    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    varCountNum = 10

    @IBOutlet weak var label: UILabel!

    @ IBAction func button (sender: AnyObject) {
        CountNum --
        ifCountNum == 0 {
            label.text="0 now"
        } else {
            label.text=String (CountNum)
        }
    }
}

I think this is a good idea.


2022-09-30 18:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.