I am trying to follow a YouTube tutorial and learn Swift by making a simple card game. After completing the tutorial I wanted to add some features of my own such as resetting the score and cards once the app is opened. When I say opened I mean either entering the foreground or launching the app. I am currently attempting this by creating a method called reset
in ViewController.swift
and calling the method in AppDelegate.swift
when the function applicationWillEnterForeground
is called. I am able to build the code successfully and run it however when the app comes to foreground I get an error which states "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value".
AppDelegate.swift:
//
// AppDelegate.swift
// War
//
// Created by Rafael on 3/17/18.
// Copyright © 2018 Rafael. All rights reserved.
//
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate{
var window: UIWindow?
var ViewControl: ViewController = ViewController()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
print("Launching app!")
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
print("Going inactive!")
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
print("Entering background!")
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
print("Entering Foreground!")
ViewControl.reset()
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
print("Going active!")
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
print("Going to terminate!")
}
}
enter code here
ViewController.swift:
//
// ViewController.swift
// War
//
// Created by Rafael on 3/17/18.
// Copyright © 2018 Rafael. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var rightImageView: UIImageView!
@IBOutlet weak var leftImageView: UIImageView!
@IBOutlet weak var leftScoreLabel: UILabel!
var leftScore = 0
@IBOutlet weak var rightScoreLabel: UILabel!
var rightScore = 0
let cardNames = ["card2", "card3", "card4", "card5", "card6", "card7", "card8", "card9", "card10", "card11", "card12", "card13", "card14"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
print("loading view!")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func dealTapped(_ sender: Any) {
let leftNumber = Int(arc4random_uniform(13))
let rightNumber = Int(arc4random_uniform(13))
if leftNumber > rightNumber {
leftScore += 1
leftScoreLabel.text = String(leftScore)
} else if(rightNumber == leftNumber){
// Do nothing - TIE
} else{
rightScore += 1
rightScoreLabel.text = String(rightScore)
}
leftImageView.image = UIImage(named: cardNames[leftNumber])
rightImageView.image = UIImage(named: cardNames[rightNumber])
}
func reset() {
print("resetting!")
leftImageView.image = UIImage(named: "back")
rightImageView.image = UIImage(named: "back")
rightScoreLabel.text = "0"
leftScoreLabel.text = "0"
rightScore = 0
leftScore = 0
}
}
All outlets are working fine and the app works fine. The only issue is when the function reset
is called. How can I make my app reset the cards when the app is going to be in view? I am still learning the language.