UILabel returns "nil"? - swift

I am having trouble with my UIButton. For Example, when I click the the UIButton Number3 and hit the checkButton on the ViewController the result in the currentCountLabel is Optional(3). It does this for every number after I click the checkButton. If I then click the button again, the currentCountLabel says, "nil". Here is all of my code:
ViewController.swift:
class ViewController: UIViewController {
var currentCountLabel: UILabel = UILabel()
var currentCount: Int = 0
var IncorrectLabel: UILabel = UILabel()
var CorrectLabel: UILabel = UILabel()
var currectAmountCorrect : Int = 0
var currentAmountIncorrect : Int = 0
var flashTimer : NSTimer = NSTimer()
var timerCounter : Int = 0
var randomImageGeneratorNumber : Int = 0
var flashingImageView: UIImageView = UIImageView()
var flashButton: UIButton = UIButton()
var currentTime = NSTimer()
func timerFunc() {
flashButton.hidden = !flashButton.hidden
flashingImageView.hidden = !flashingImageView.hidden
}
#IBAction func flashButton(sender: UIButton) {
flashButton.hidden = !flashButton.hidden
flashingImageView.hidden = !flashingImageView.hidden
flashingImageView.image = UIImage(named: "Image\(arc4random_uniform(6) + 1).png")
if (flashingImageView.image == UIImage(named: "Image1")){
randomImageGeneratorNumber == 1
}
if (flashingImageView.image == UIImage(named: "Image2")){
randomImageGeneratorNumber == 2
}
if (flashingImageView.image == UIImage(named: "Image3")){
randomImageGeneratorNumber == 3
}
if (flashingImageView.image == UIImage(named: "Image4")){
randomImageGeneratorNumber == 4
}
if (flashingImageView.image == UIImage(named: "Image5")){
randomImageGeneratorNumber == 5
}
if (flashingImageView.image == UIImage(named: "Image6")){
randomImageGeneratorNumber == 6
}
currentCount = 0
currentCountLabel.text = "\(currentCount)"
if (flashButton.hidden == true){
flashButton.hidden = true;
flashingImageView.hidden = false
}
var currentTime = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "timerFunc", userInfo: nil, repeats: false)
func timerFunc(){
self.flashingImageView.hidden = true;
flashButton.hidden = !flashButton.hidden
flashingImageView.hidden = !flashingImageView.hidden
}
}
#IBAction func checkButton(sender: UIButton) {
if (flashButton.hidden == true){
flashButton.hidden == false
flashingImageView.hidden == true
}
var currentCount = currentCountLabel.text?.toInt()
if randomImageGeneratorNumber == currentCount {
currectAmountCorrect += 1
CorrectLabel.text = "\(currectAmountCorrect)"
} else {
currentAmountIncorrect += 1
IncorrectLabel.text = "\(currentAmountIncorrect)"
}
if (currentCountLabel.text == "0"){
let alert = UIAlertView()
alert.title = "Alert"
alert.message = "You must type in an answer in order to check it"
alert.addButtonWithTitle("Understood")
alert.show()
}
currentCount == 0
currentCountLabel.text = "\(currentCount)"
}
#IBAction func clearButton(sender: UIButton) {
currentCount = 0
currentCountLabel.text = "\(currentCount)"
}
#IBAction func Number1Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 1
currentCountLabel.text = "\(currentCount)"
}
#IBAction func Number2Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 2
currentCountLabel.text = "\(currentCount)"
}
#IBAction func Number3Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 3
currentCountLabel.text = "\(currentCount)"
}
#IBAction func Number4Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 4
currentCountLabel.text = "\(currentCount)"
}
#IBAction func Number5Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 5
currentCountLabel.text = "\(currentCount)"
}
#IBAction func Number6Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 6
currentCountLabel.text = "\(currentCount)"
}
override func viewDidLoad() {
super.viewDidLoad()
var currentCountLabel = "\(currentCount)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I have no idea what so ever is happening or how to fix it.
Any input and suggestions would be greatly appreciated.
Thanks in advance.

It would help if you clarified how you want it to behave. If you're wondering why it's an optional and why it's returning nil then you can fix this as follows.
In your checkButton() function you have this line at the end:
currentCountLabel.text = "\(currentCount)"
Unwrap the currentCount variable to this:
currentCountLabel.text = "\(currentCount!)"
Within this same checkButton() function you have this line:
currentCount == 0
So if you're wondering why it's not returning 0 it's because you're using a comparison operator not an assignment operator.

Related

Array Issue with Display Label

Everything appears to be working in this tic tac toe game, except for displaying the "draw" label when there is no winner. The label will switch when Cross or circles wins, but not when there is a tie.
I'm stumped. E
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
var count = 1
var activePlayer = 1 //Cross
var gameState = [0,0,0,0,0,0,0,0,0]
var gameIsActive = true
let winningCombinations = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]]
#IBAction func action(_ sender: AnyObject) {
if (gameState[sender.tag-1] == 0 && gameIsActive == true) {
gameState[sender.tag-1] = activePlayer
if (activePlayer == 1) {
sender.setImage(UIImage(named: "cross.png"), for: UIControl.State())
activePlayer = 2
} else {
sender.setImage(UIImage(named: "nought.png"), for: UIControl.State())
activePlayer = 1
}
}
for combination in winningCombinations {
if gameState[combination[0]] != 0 &&
gameState[combination[0]] == gameState[combination[1]] &&
gameState[combination[1]] == gameState[combination[2]] {
gameIsActive = false
if gameState[combination[0]] == 1 {
label.text = "Cross has won!"
} else {
label.text = "Circle has won!"
}
if gameIsActive == true {
for i in gameState {
count = i*count
}
if count != 0 {
label.text = "It was a draw."
label.isHidden = false
playAgainButton.isHidden = false
}
}
playAgainButton.isHidden = false
label.isHidden = false
}
}
} // End Button Action
#IBOutlet weak var playAgainButton: UIButton!
#IBAction func playAgain(_ sender: Any) {
gameState = [0,0,0,0,0,0,0,0,0]
gameIsActive = true
activePlayer = 1
playAgainButton.isHidden = true
label.isHidden = true
for i in 1...9 {
let button = view.viewWithTag(i) as! UIButton
button.setImage(nil, for: UIControl.State())
}
}
override func viewDidLoad() {
super.viewDidLoad()
playAgainButton.isHidden = true
label.isHidden = true
}
} // End ViewController
You are saying
gameIsActive = false
// ... some other stuff ...
if gameIsActive == true {
// check for a draw
}
But gameIsActive is not true, because you just set it to false. Therefore we never perform the check for a draw.
Thanks for the help everyone! I finally got it working correctly.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var playAgainButton: UIButton!
#IBOutlet weak var label: UILabel!
var count = 1
var activePlayer = 1 //Cross
var gameState = [0,0,0,0,0,0,0,0,0]
var gameIsActive = true
let winningCombinations = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]]
#IBAction func action(_ sender: AnyObject) {
count = 1;
if (gameState[sender.tag-1] == 0 && gameIsActive == true) {
if (activePlayer == 1) {
gameState[sender.tag-1] = activePlayer
sender.setImage(UIImage(named: "cross.png"), for: UIControl.State())
activePlayer = 2
} else {
sender.setImage(UIImage(named: "nought.png"), for: UIControl.State())
gameState[sender.tag-1] = activePlayer
activePlayer = 1
}
}
for combination in winningCombinations {
if gameState[combination[0]] != 0 && gameState[combination[0]] ==
gameState[combination[1]] && gameState[combination[1]] ==
gameState[combination[2]]{
gameIsActive = false
if gameState[combination[0]] == 1 {
label.text = "Cross has won!"
} else {
label.text = "Circle has won!"
}
playAgainButton.isHidden = false
label.isHidden = false
}
}
if gameIsActive == true{
for i in gameState{
count = i * count
}
if count != 0{
label.text = "DRAW!"
playAgainButton.isHidden = false
label.isHidden = false
}
}
}
#IBAction func playAgain(_ sender: Any) {
gameState = [0,0,0,0,0,0,0,0,0]
gameIsActive = true
activePlayer = 1
playAgainButton.isHidden = true
label.isHidden = true
for i in 1...9
{
let button = view.viewWithTag(i) as! UIButton
button.setImage(nil, for: UIControl.State())
}
}
override func viewDidLoad() {
super.viewDidLoad()
playAgainButton.isHidden = true
label.isHidden = true
}
}

Adding a Decimal Point button to the calculator app

Can someone please add the code to this so I'm able to add a decimal point button?
This is what I have so far, can someone please fill in the code required for the decimal point button. so I can learn from it. please view over the code and let me know what you think.
import UIKit
class ViewController: UIViewController {
var numberOnScreen:Double = 0;
var previousNumber:Double = 0;
var preformingMath = false
var operation = 0;
#IBOutlet weak var label: UILabel!
#IBAction func numbers(_ sender: UIButton)
{
if preformingMath == true
{
label.text = String(sender.tag-1)
numberOnScreen = Double(label.text!)!
preformingMath = false
}
else
{
label.text = label.text! + String(sender.tag-1)
numberOnScreen = Double(label.text!)!
}
}
#IBAction func buttons(_ sender: UIButton)
{
if label.text != "" && sender.tag != 11 && sender.tag != 16
{
previousNumber = Double(label.text!)!
if sender.tag == 12 //Divide
{
label.text = "/";
}
else if sender.tag == 13//Multiply
{
label.text = "x";
}
else if sender.tag == 14 //Minus
{
label.text = "-";
}
else if sender.tag == 15 //Plus
{
label.text = "+";
}
operation = sender.tag;
preformingMath = true;
}
else if sender.tag == 16
{
if operation == 12
{
label.text = String(previousNumber / numberOnScreen)
}
else if operation == 13
{
label.text = String(previousNumber * numberOnScreen)
}
else if operation == 14
{
label.text = String(previousNumber - numberOnScreen)
}
else if operation == 15
{
label.text = String(previousNumber + numberOnScreen)
}
}
else if sender.tag == 11
{
label.text = ""
previousNumber = 0;
numberOnScreen = 0;
operation = 0;
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Add this #IBAction to your code and hook it to your . button.
If the user presses . before any numbers, the display will be set to 0..
If there are numbers in the display, then . will be appended to the display if there isn't already a decimal point in the number.
Finally, the numberOnScreen will be updated.
#IBAction func decimal(_ sender: UIButton) {
if preformingMath || label.text!.isEmpty
{
label.text = "0."
preformingMath = false
}
else
{
if !label.text!.contains(".") {
label.text = label.text! + "."
}
}
numberOnScreen = Double(label.text!)!
}

Multiplying, subtracting and dividing binary numbers give wrong answers

I'm making a binary calculator app.
No buildtime issues but in the app only adding binary numbers give the right answer, multiplying, subtracting and dividing don't.
Dividing - every answer = 1 (1010-0001 = 1; 10101010-1010 = 1)
Multiplying - every answer is wrong, (11111 x 101010 = 1111000001, should be 10100010110)
Subtracting - every answer = 0 ( 1010-0001 = 0; 1010101-0001 = 0)
I tried making the code more explanatory and it worked in the playground (ending with print() ) but when I changed the apps code, the answers were the same.
var numberOnScreen:Int = 0;
var previousNumber:Int = 0;
var doingMath = false
var operation = 0;
var decimal = 0;
var decimal1 = 0;
var binary:String = ""
var binary1:String = ""
#IBOutlet weak var label: UILabel!
#IBAction func Numbers(_ sender: UIButton) {
if doingMath == true
{
label.text = String(sender.tag-1)
numberOnScreen = Int(label.text!)!
doingMath = false
}
else
{
label.text = label.text! + String(sender.tag-1)
numberOnScreen = Int(label.text!)!
}
}
#IBAction func buttons(_ sender: UIButton) {
if label.text != "" && sender.tag != 6 && sender.tag != 8
{
previousNumber = Int(label.text!)!
binary = "\(previousNumber)"
decimal = Int(binary, radix: 2)!
binary1 = "\(numberOnScreen)"
decimal1 = Int(binary1, radix: 2)!
operation = sender.tag
doingMath = true;
}
else if sender.tag == 8
{
if operation == 3 //adding
{
label.text = String((decimal + decimal1), radix: 2)
}
else if operation == 4 //subtracting
{
label.text = String((decimal - decimal1), radix: 2)
}
else if operation == 5 //multiplying
{
label.text = String((decimal * decimal1), radix: 2)
}
else if operation == 7 //dividing
{
label.text = String((decimal / decimal1), radix: 2)
}

Second variable takes the firsts value

I built an binary calculator app, For the numbers I have two variables. previousNumber and numberOnScreen. The idea is to convert the binary numbers to decimals, do the calculation and convert the answer back.
Lets say the first (previousNumber)number I pick is 1010 and the second (numberOnScreen) 10100
var numberOnScreen:Int = 0;
var previousNumber:Int = 0;
var doingMath = false
var operation = 0;
var decimal = 0;
var decimal1 = 0;
var binary:String = ""
var binary1:String = ""
#IBOutlet weak var label: UILabel!
#IBAction func Numbers(_ sender: UIButton) {
if doingMath == true
{
label.text = String(sender.tag-1)
numberOnScreen = Int(label.text!)!
doingMath = false
}
else
{
label.text = label.text! + String(sender.tag-1)
numberOnScreen = Int(label.text!)!
}
}
#IBAction func buttons(_ sender: UIButton) {
if label.text != "" && sender.tag != 6 && sender.tag != 8
{
previousNumber = Int(label.text!)!
binary = "\(previousNumber)"
decimal = Int(binary, radix: 2)!
binary1 = "\(numberOnScreen)"
decimal1 = Int(binary1, radix: 2)!
operation = sender.tag
doingMath = true;
}
else if sender.tag == 8
{
if operation == 3 //adding
{
print(previousNumber, numberOnScreen, decimal, decimal1)
It prints [ 1010, 10100, 10, 10 }
Any ideas why this is happening?
When you hit the = key, you need to update the calculation of decimal1. Move that code to the place where you are processing =:
#IBAction func buttons(_ sender: UIButton) {
if label.text != "" && sender.tag != 6 && sender.tag != 8
{
previousNumber = Int(label.text!)!
binary = "\(previousNumber)"
decimal = Int(binary, radix: 2)!
operation = sender.tag
doingMath = true;
}
else if sender.tag == 8
{
binary1 = "\(numberOnScreen)"
decimal1 = Int(binary1, radix: 2)!
if operation == 3 //adding
{
print(previousNumber, numberOnScreen, decimal, decimal1)
Also, I recommend that you get rid of the magic numbers in your code and replace them with constants. For your key tags, you could define a struct like this:
struct Key {
static let clear = 6
static let equals = 8
static let plus = 3
}
and then your code would read:
if label.text != "" && sender.tag != Key.clear && sender.tag != Key.equals {
which is much clearer.

Timer Going too fast after first quiz Swift 3

i have quiz app and have 10 sec timer each question. First question timer is fine, but in second question it going faster than before. I try this tutorial to make timer and this tutorial to make quiz app
here's my code :
override func viewDidLoad() {
timerBegin()
}
func timerBegin() {
myTimer.invalidate() // print error `fatal error: unexpectedly found nil while unwrapping an Optional value`
timeLeft = 10
myTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(SoalViewController.timerRunning), userInfo: nil, repeats: true)
}
func timerRunning() {
timeLeft -= 1
timeLabel.text = "\(timeLeft)"
if timeLeft == 0 {
myTimer.invalidate()
timeLabel.text = "10"
pickQuestion()
timerBegin()
}
}
here's my option button code :
#IBAction func opsiA(_ sender: Any) {
timeLabel.text = "10"
timerBegin()
if AnswerNumber == 0 {
pickQuestion()
Score += 10
scoreLabel.text = "\(Score)"
} else {
pickQuestion()
print("Wrong")
}
}
#IBAction func opsiB(_ sender: Any) {
timeLabel.text = "10"
timerBegin()
if AnswerNumber == 1 {
pickQuestion()
Score += 10
scoreLabel.text = "\(Score)"
} else {
pickQuestion()
print("Wrong")
}
}
#IBAction func opsiC(_ sender: Any) {
timeLabel.text = "10"
timerBegin()
if AnswerNumber == 2 {
pickQuestion()
Score += 10
scoreLabel.text = "\(Score)"
} else {
pickQuestion()
print("Wrong")
}
}
#IBAction func opsiD(_ sender: Any) {
timeLabel.text = "10"
timerBegin()
if AnswerNumber == 3 {
pickQuestion()
Score += 10
scoreLabel.text = "\(Score)"
} else {
pickQuestion()
print("Wrong")
}
}
and this is my pick question function :
func pickQuestion() {
if Questions.count > 10 {
QNumber = Int(arc4random_uniform(UInt32(10)))
textView.text = Questions[QNumber].Question
AnswerNumber = Questions[QNumber].Answer
for i in 0..<Buttons.count {
Buttons[i].setTitle(Questions[QNumber].Answers[i], for: UIControlState.normal)
}
Questions.remove(at: QNumber)
} else {
darkView.isHidden = false
scoreLabel.text = "\(Score)"
timeLabel.isHidden = true
if Score > standStill {
HighScore = Score
highScoreLabel.text = NSString(format: "Highscore : %i", HighScore) as String
let currentHighScore = UserDefaults.standard
currentHighScore.set(HighScore, forKey: "HighScore")
} else {
let currentHighScore = UserDefaults.standard
currentHighScore.set(standStill, forKey: "HighScore")
}
}
}
i already try to stop timer by adding myTimer.invalidate() in first line of timerBegin function but it print fatal error: unexpectedly found nil while unwrapping an Optional value