//
// ViewController.swift
// SimpleCounterApp
//
// Created by 영현 on 12/12/23.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBOutlet weak var countLabel: UILabel!
@IBAction func countUpButton(_ sender: UIButton) {
if var count = self.countLabel.text {
if var count = Int(count) {
count = (Int(count) + 1)
self.countLabel.text = String(count)
}
}
}
@IBAction func countDownButton(_ sender: Any) {
if var count = self.countLabel.text {
if var count = Int(count) {
count = (Int(count) - 1)
self.countLabel.text = String(count)
}
}
}
}
countLabel
으로 네이밍했다.count
라는 변수를 선언하고, 옵셔널 바인딩을 진행한 다음 1씩 증가시키고 self.countLabel.text
에 String
형식으로 저장하는 함수를 구성했다.@IBAction
함수 내부에 선언했는데, 클래스 내부에 private var
형태의 변수를 만들었으면 불필요한 옵셔널 바인딩을 진행하지 않았어도 되었을 것 같다.count
변수가 변화할 때 마다 UILabel
에 표시할 수 있도록 만들 수 있을 것 같다.개선을 진행한 다음에 이어서 내용을 추가하도록 해보겠다.