I'm getting into Swift iOS development, and decided to create a Cookie Clicker application as a first attempt. Since I am very new with Swift, I would like feedback and criticism on everything possible, to kick bad habits to the curb early. Any and all feedback is welcome, appreciated and considered!
Since all of the code that I wrote is contained in the ViewController
class, I'm only posting the class. If needed, I can post other classes. A screenshot of the layout of my app will also be available below.
ViewController.swift
//MARK: Import Statements
import UIKit
class ViewController: UIViewController {
//MARK: Properties
//Labels
@IBOutlet weak var cookiesPerClick: UILabel!
@IBOutlet weak var numberOfCookies: UILabel!
@IBOutlet weak var mainCookie: UILabel!
//Buttons
@IBOutlet weak var upgradeButton: UIButton!
@IBOutlet weak var cookieButton: UIButton!
//Cookie Stuff
var cookies: Int = 0
var cookiesAClick: Int = 1
var cookieUpgradeCost: Int = 10
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
formatItems()
}
//MARK: Actions
@IBAction func upgradeClicker(_ sender: UIButton) {
if (cookies >= cookieUpgradeCost) {
cookiesAClick += 1
cookies -= cookieUpgradeCost
cookieUpgradeCost *= 2
upgradeButton.setTitle(
"Upgrade Cookie | Cost: \(cookieUpgradeCost) Cookies",
for: .normal
)
numberOfCookies.text = "\(cookies)"
cookiesPerClick.text = "Cookie Per Click: \(cookiesAClick)"
}
}
@IBAction func getCookie(_ sender: UIButton) {
cookies += cookiesAClick
numberOfCookies.text = "\(cookies)"
}
//MARK: Local Functions
func formatItems() {
//Upgrade Button Formatting
upgradeButton.layer.borderWidth = 1
upgradeButton.layer.cornerRadius = 5
upgradeButton.layer.borderColor = UIColor.black.cgColor
//Get Cookie Button Formatting
cookieButton.layer.borderWidth = 1
cookieButton.layer.cornerRadius = 5
cookieButton.layer.borderColor = UIColor.black.cgColor
//Number Of Cookies Label Formatting
numberOfCookies.layer.borderWidth = 1
numberOfCookies.layer.cornerRadius = 5
numberOfCookies.layer.borderColor = UIColor.black.cgColor
//Set main cookie to bold
mainCookie.font = UIFont.boldSystemFont(ofSize: 35.0)
}
}
App Layout Screenshot