Changing uiLabel value on field change Swift (newbie) - swift

Disclaimer: First iOS/ Swift app and following a tutorial.
I am trying to update the value of a uilabel based on what I enter in the uiField. In the debugger I see it entering the onEditingChanged() but the values of the label never change... No errors thrown. Also, the initial assignment to tipLabel.text = "$0.00" and totalLabel.text = "$0.00" works fine...
Any suggestions what may be wrong?
// ViewController.swift
// tips
//
// Created by Jonida Cali on 12/13/14.
// Copyright (c) 2014 Jonida Cali. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var amountField: UITextField!
#IBOutlet weak var tipLabel: UILabel!
#IBOutlet weak var totalLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tipLabel.text = "$0.00"
totalLabel.text = "$0.00"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func onEditingChanged(sender: AnyObject) {
println("changed")
var billAmount = (amountField.text as NSString).doubleValue
var tip = billAmount*0.2
var total = billAmount + tip
tipLabel.text = String(format: "$%.2f", tip)
totalLabel.text = String(format: "$%.2f",total)
}
}

Related

Cannot convert value of type 'String?' to type 'NSString' in coercion

Im trying to make this calculator for various math formulas and I'm stuck at this point. I was following this tutorial
Here's my code:
import UIKit
class pythagorasViewController: UIViewController {
#IBOutlet weak var aLabel: UILabel!
#IBOutlet weak var bLabel: UILabel!
#IBOutlet weak var aField: UITextField!
#IBOutlet weak var bField: UITextField!
#IBOutlet weak var answerLabel: UILabel!
#IBAction func calculateButton(_ sender: UIButton) {
var a = (aField.text as NSString).floatValue
var b = (bField.text as NSString).floatValue
var answer = sqrt(a*a + b*b)
answerLabel.text = "\(answer)"
}
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.
}
}
The part where I'm getting the error is at:
var a = (aField.text as NSString).floatValue
var b = (bField.text as NSString).floatValue
Prefer let to var when possible. You do not need to use NSString. You can cast String to Float?. You need to unwrap both the text property which is a String? (if you have a question about the type of a variable option click and it will show you) and the Float? conversion:
func calculateButton(_ sender: UIButton) {
guard let aText = aField.text,
let bText = bField.text,
let a = Float(aText),
let b = Float(bText) else {
return
}
let answer = sqrt(a*a + b*b)
answerLabel.text = "\(answer)"
}

Xcode 10 button to create mad lips story will not run, I'm new to Xcode

I'm just starting out with Xcode 10 and cannot get this button to work for my mad libs creation. this is just for fun but I would love to know how to fix it. Please look over my code to see why the "create story" button is not active.
// ViewController.swift
// Field Button Fun
//
// Created by Audrey Chao on 8/3/16.
// Copyright © 2016 Maddie Chao. All rights reserved.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var thePlace: UITextField!
#IBOutlet weak var theVerb: UITextField!
#IBOutlet weak var theNumber: UITextField!
#IBOutlet weak var theTemplate: UITextView!
#IBOutlet weak var theStory: UITextView!
#IBAction func createStory(sender: AnyObject) {
theStory.text = theTemplate.text
theStory.text = theStory.text.stringByReplacingOccurrencesOfString("<place>", withString: thePlace.text!)
theStory.text = theStory.text.stringByReplacingOccurrencesOfString("<verb>", withString: theVerb.text!)
theStory.text = theStory.text.stringByReplacingOccurrencesOfString("<number>", withString: theNumber.text!)
}
override func viewDidLoad() {
super.viewDidLoad()
//thePlace.resignFirstResponder()
// 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.
}
}
Have you wired up your button to your IBAction? If they aren't wired up, Xcode won't be able to tell what your button is supposed to do.

comparing local images swift

"Optional type Bool cannot be used as a boolean; test for !nil instead"
Is the error I'm getting
I'm trying to make a "slot machine" app, very basic,
You press the UIButton, and the three images should all change, randomly, if the 3 matches, print "You won!"
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var img1: UIImageView!
#IBOutlet weak var img2: UIImageView!
#IBOutlet weak var img3: UIImageView!
#IBOutlet weak var rollBtn: UIButton!
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 onRollPress(sender: AnyObject) {
let randomRoll = ImgArray().getRandomImage()
img1.image = randomRoll
img2.image = randomRoll
img3.image = randomRoll
if (img1.image! == img2 && img3) {
print("You won!")
}
}
}
If your images are in an array, why not get the indexes of the images and compare that. Or you could convert the images to base64 strings and compare those.
Edit: Not knowing much about your ImgArray class, this may work for you:
#IBAction func onRollPress(sender: AnyObject) {
let randomRoll1 = ImgArray().getRandomImage()
let randomRoll2 = ImgArray().getRandomImage()
let randomRoll3 = ImgArray().getRandomImage()
img1.image = randomRoll1
img2.image = randomRoll2
img3.image = randomRoll3
let imgIndex1 = ImgArray().indexOf(randomRoll1)
let imgIndex2 = ImgArray().indexOf(randomRoll2)
let imgIndex3 = ImgArray().indexOf(randomRoll3)
if (imgIndex1 == imgIndex2 && imgIndex3) {
print("You won!")
}
}

How to send my UITextfield to a variable via an UIButton?

I want to know how can I type something in my UITextfield and then send it to a variable via an UIButton?
When I try it with this code the app automatically crashes and show me this error:
Thread:1 signal SIGABRT
Here's the code:
import UIKit
import Foundation
class ViewController: UIViewController {
#IBOutlet weak var LabelHeure: UILabel!
#IBOutlet weak var Heure: UITextField!
#IBAction func Send(sender: AnyObject) {
var one = String(Heure.text)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
LabelHeure.text = NSDateFormatter.localizedStringFromDate(NSDate(),dateStyle:NSDateFormatterStyle.MediumStyle,timeStyle: NSDateFormatterStyle.NoStyle)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You don't need to cast Heure.text as a string because it is already a string.
Here is the code I used and it worked. After pressing the button, the variable is updated with the text from the textfield. I used a println to verify.
import UIKit
import Foundation
class ViewController: UIViewController {
#IBOutlet weak var LabelHeure: UILabel!
#IBOutlet weak var Heure: UITextField!
#IBAction func Send(sender: AnyObject) {
var myVariable = Heure.text
println(myVariable)
}
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.
}
}

Thread1: Exc_Bad_Instruction

I can't figure out why am I keep getting this Thread:
Error message when running this program.
I'm new to programming and trying an exercise and keep getting this error message.
Also I was trying to figure out how to resize the pictures to fit on the screen.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var memberChoice: UISegmentedControl!
#IBOutlet weak var webView: UIWebView!
#IBAction func getMember(sender:AnyObject){
var member: String =
memberChoice.titleForSegmentAtIndex(memberChoice.selectedSegmentIndex)!
var imageURLString: String!
if member == "Daddy" {
imageURLString = "file: ///Users/natashamays/Desktop/Daddy.jpg"
}
else if member == "Tasha" {
imageURLString = "https://lh3.googleusercontent.com/-2c_GDVdcAFk/UBR7auHIHrI/AAAAAAAAEW8/gJ3F-MVUpL4/w140-h139-p/3.jpg"
}
else if member == "Jasmin" {
imageURLString = "file:///Users/natashamays/Desktop/IMG_3879.JPG"
}
var imageURL: NSURL = NSURL(string:imageURLString)!
webView.loadRequest(NSURLRequest(URL: imageURL))
}
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.
}