Swift Xcode 6 baseball Counter - ios

I can't seem to write the if statement in the end to make the outs go up when the strikes hit 3?
can someone lease help and make it so in swift the outs will go up when someone hits 3 strikes
//
// ViewController.swift
// helloWordDemo
//
// Created by Developer on 6/8/14.
// Copyright (c) 2014 AECApps. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet var labelDispaly : UILabel = nil
// dispaly Strikes
var counter = 1
#IBAction func buttonPressed(sender : AnyObject) {
labelDispaly.text = "Strikes \(counter++)"
}
//button to add strikes
#IBOutlet var OutsDispaly : UILabel = nil
var outsCounter = 1
//outs dispaly
#IBAction func outsButtonPressed(sender : AnyObject) {
OutsDispaly.text = "Outs \(outsCounter++)"
}
//button to add outs
if counter = 3 {
outsCounter ++
}
}

Use a property observer:
var counter = 1 {
didSet {
if counter == 3 {
self.outsCounter++
}
}
}
Whenever counter gets changed, didSet will be called.
(Also note that the equality operator is ==. = is for assignment.)

Related

Trying to get a quiz function to work in swift

I'm trying to get a quiz function to work, and my problem is that I'm trying to print the score on a second view controller. When i run the simulator it takes the user to the second view controller yet the score does not print.
import UIKit
var CurrentQuestion = "" // global variable to be accessed
class ViewController: UIViewController {
// create an array to store queations
let questions = ["favourite pet?", "favourite colour", "where was i born"]
// multiple arrays that contain strings, right anwser is first
let anwsers = [["dog", "cat", "bird"], ["blue", "black", "green"], ["tokyo", "tennese", "england"]]
// variables
// keeps track of what question were at
var currentQuestion = 0
var rightAnwserPlacement:UInt32 = 0 // where right anwser is located
var points = 0; // counts number of points - score
//lbl
#IBOutlet weak var lbl: UILabel!
//button
#IBAction func btn(_ sender: UIButton) {
if (sender.tag == Int(rightAnwserPlacement))
{
print ("right")
points += 1
}
else
{
print ("wrong")
}
if (currentQuestion != questions.count)
{
newQuestion()
}
else
{
performSegue(withIdentifier: "showScore", sender: self)
}
}
override func viewDidAppear(_ animated: Bool) {
newQuestion()
}
// function that displays a new question sets up interface for the new questions
func newQuestion()
{
lbl.text = questions[currentQuestion]
rightAnwserPlacement = arc4random_uniform(3)+1 // random button
// create a button
var button:UIButton = UIButton()
var x = 1
for i in 1...3
{
// create a button
button = view.viewWithTag(i) as! UIButton
if (i == Int(rightAnwserPlacement)) // checks to see if it matches the right anwser
{
button.setTitle(anwsers[currentQuestion][0], for: .normal)
}
else {
button.setTitle(anwsers[currentQuestion][x], for: .normal)
x = 2
}
}
currentQuestion += 1
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The second view controller is
import UIKit
class second: UIViewController {
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
label.text = CurrentQuestion
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
You have set CurrentQuestion as a global string variable and later on try to increment its value by 1? What is that supposed to mean? Additionally, you have a local variable in the class with the same name as currentQuestion. Granted the two are different variables, but you should seriously consider renaming your variables.
Also, you are changing value of this local variable and no-where changing the value of the global variable. Most likely this mix-up is because of similar naming of variables.
And just a suggestion, avoid using global variables to pass data. Look into delegate-protocol way of passing data between VCs.

Bind a switch state to a function in Swift

I'm learning swift by building a basic counting app (based on this tutorial https://www.youtube.com/watch?v=3blma4PCRak) and I'm trying to have a function that counts on increments of 3 based on the state of a switch.
It seems a lot of the answers I find in tutorials online and from SO are from many years ago and a lot of the syntax is deprecated, including:
• instead of oddSwitch.On now it is oddSwitch.isOn
• Stating colors changed from UIColor.redColor to just UIColor.red
And so on...below is my code thus far:
//
// ViewController.swift
// TestApp
//
// Created by kawnah on 2/8/17.
// Copyright © 2017 kawnah. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet var outputLabel: UILabel? = UILabel();
var currentCount = 0;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func addOneBUtton(_ sender: UIButton) {
currentCount = currentCount + 1
if currentCount <= 1 {
outputLabel?.text = "\(currentCount) click"
} else {
outputLabel?.text = "\(currentCount) clicks"
}
outputLabel?.textColor = UIColor.red
}
#IBOutlet var oddSwitch: UISwitch!
#IBAction func oddToggle(_ sender: UISwitch) {
if oddSwitch!.isOn {
currentCount = currentCount + 3
}
}
}
I'm confused as to the relationship between the #IBOutlet and my function that states to count in increments of three. I've tried including weak storage as well but it's to my understanding that is the default for #IBOutlet. Currently when I toggle the switch on, the counter still increments by 1.
My error log simply shows compression errors for PNGs for my splash screen, nothing with the code. What exactly is happening?
Change your addOneBUtton code to match this.
#IBAction func addOneBUtton(_ sender: UIButton) {
if oddSwitch!.isOn {
currentCount = currentCount + 3
} else {
currentCount = currentCount + 1
}
if currentCount <= 1 {
outputLabel?.text = "\(currentCount) click"
} else {
outputLabel?.text = "\(currentCount) clicks"
}
outputLabel?.textColor = UIColor.red
}
And you can remove this section.
#IBAction func oddToggle(_ sender: UISwitch) {
if oddSwitch!.isOn {
currentCount = currentCount + 3
}
}
So the complete code should look something like this.
import UIKit
class testViewController: UIViewController {
//MARK: IBOutlets
#IBOutlet var outputLabel: UILabel? = UILabel();
#IBOutlet var oddSwitch: UISwitch!
//MARK: Vars
var currentCount = 0;
//MARK: Lifecyle
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//MARK: IBActions
#IBAction func addOneBUtton(_ sender: UIButton) {
if oddSwitch!.isOn {
currentCount = currentCount + 3
} else {
currentCount = currentCount + 1
}
if currentCount <= 1 {
outputLabel?.text = "\(currentCount) click"
} else {
outputLabel?.text = "\(currentCount) clicks"
}
outputLabel?.textColor = UIColor.red
}
}
Note: This was my twist on keeping the code clean with IBOutlets all in one spots and using //MARK: to the sections of code quicker to jump to.

Making a basic tally counter in iOS

I am having difficulty in making simple tally counter application. I have put the codes but when i hit "run simulation", it always gives error message. can anyone help me find my mistake? thank you.
my codes are like below:
import UIKit
class ViewController: UIViewController {
var counter: Int!
#IBOutlet weak var Display: UILabel!
func updateDisplay() {
Display.text = String(counter)
}
override func viewDidLoad() {
super.viewDidLoad()
counter = 0
updateDisplay()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func PlusButton(sender: AnyObject) {
counter = counter + 1
updateDisplay()
}
#IBAction func MinusButton(sender: AnyObject) {
counter = counter - 1
updateDisplay()
}
#IBAction func ClearButton(sender: AnyObject) {
counter = 0
updateDisplay()
}
}
here is the appearance in the Xcode
https://www.facebook.com/photo.php?fbid=10204747519271053&set=pcb.10204747520111074&type=1&theater
here is the error message when i hit run simulation
https://www.facebook.com/photo.php?fbid=10204747519351055&set=pcb.10204747520111074&type=1&theater
Your code is working fine but I think you have problem with layOut connection so check it again and it should be like this:
For Display label:
For PlusButton :
For MinusButton:
And For ClearButton:
Check THIS sample project for more Info.

An instance of a view in ios

I'm having a difficulty understanding what output.text means. I think my understanding is that output is an instance of the class UIViewController and text is a variable defined in this class. Is this correct? Also, in the function, consoleOut(), why should one do output.text = output.text + text instead of output.text = text?
//
// ViewController.swift
// Guessing Game
//
// Created by Jae Hyun Kim on 8/6/15.
// Copyright (c) 2015 Jae Hyun Kim. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var inputField: UITextField!
#IBOutlet weak var output: UITextView!
var guesses : UInt = 0;
var number : UInt = 0;
var gameover = false;
let MAX_GUESSES = 8;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func consoleOut(text : String) {
output.text = output.text + text;
}
#IBAction func guess(sender: UIButton) {
}
}
output is a UITextView that is defined in the view (the file that shows what screen will appear to the user). So you are correct in saying that text is a variable of the UITextView class.
Additionally, output.text = output.text + text will append text to the existing text in output.text. This is unlike output.text = text which will overwrite whatever was already in output.text with the new text.

ViewControllerApple.Apple.Type does not have a member named appleQuantity

I have a UITextField named 'appleQuantity', when I try to convert it to an int to do math with it, it gives me an error named 'ViewControllerApple.Apple.Type does not have a member named appleQuantity'
Here's the code, the error is in 'var b:Int=appleQuantity.text.toInt()'
import UIKit
class ViewControllerApple: UIViewController {
//80, 0, 0, 0, 22, 0, 2, 20, 2, 2
class Apple {
#IBOutlet weak var appleQuantity: UITextField!
#IBAction func addApple(sender: UIButton) {
}
var b:Int=appleQuantity.text.toInt()
// var appleCal = 80 * b
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
I'm Really new to swift, so the more details provided, the more helpful it is!
Thanks so much in advance!
UPDATED Answer:
I feel that you're thinking of things the wrong way slightly. Here is how I propose you try and achieve what you're doing.
import UIKit
class ViewControllerApple: UIViewController {
class Apple {
var b: Int?
init() {
}
func getAppleCalculation() -> Int {
if let unwrappedB = b {
return unwrappedB * 80
}
else {
return 0
}
}
}
#IBOutlet weak var appleQuantity: UITextField!
#IBAction func addApple(sender: UIButton) {
}
override func viewDidLoad() {
super.viewDidLoad()
// This is where the view has loaded. Your buttons and textviews will
// have loaded and you can perform any actions on them.
var apple = Apple()
if let unwrappedAppleQuantityInt = appleQuantity.text.toInt() {
apple.b = unwrappedAppleQuantityInt
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
OLD ANSWER: Your variable b will actually never contain anything except the default value.
You should assign your Int b in your class' viewDidLoad, or following your structure, in your object's init function:
class Apple {
#IBOutlet weak var appleQuantity: UITextField!
#IBAction func addApple(sender: UIButton) {
}
var b: Int
init() {
if let unwrappedAppleQuantityInt = appleQuantity.text.toInt() {
b = unwrappedAppleQuantityInt
}
else {
// assign b a default value
b = 0
}
}
}
or you could do return a computed value, provided you only care about the output rather than storing the value, like so:
var b:Int {
get {
if let unwrappedAppleQuantityInt = appleQuantity.text.toInt() {
return unwrappedAppleQuantityInt
}
else {
return 0
}
}
}

Resources