Unable to display text from a double to a string - ios

I'm having an issue with a simple app for iOS 9.
When I press the calculate button, the app closes and it will not change the text of the label.
What did I do wrong?
class ViewController: UIViewController {
#IBOutlet weak var weightTextField: UITextField!
#IBOutlet weak var repsTextField: UITextField!
#IBOutlet weak var resultsLabel: UILabel!
#IBAction func calculateButton(sender: AnyObject) {
let numberOfReps = Double(repsTextField.text!)!
let weight = Double(weightTextField.text!)!
let x = (numberOfReps * 0.033)
let x1 = (x + 1)
let max = (weight * x1)
let oneMaxRepString = Double(max)
resultsLabel.text = String(oneMaxRepString)
}
}

#rogue-studios is right. You didn't connect IBOutlets correctly.

I assume that your code would work. Your app could be crashing because your connection between labels/buttons are not connected correctly. Go and make sure that your #IBOutlets are connected to the storyboard objects.
If this does not work, comment the error that you are getting in the console.

Related

Looking to accept user input, and calculate a running total - Swift 5/Xcode

I am a beginner working on an app that will function like a golf scorecard. My first issue has come while attempting to accept user input in a (prototype) series of 9 textFields so the user can type in their scores, and then a textView at the end that is not editable that will show the total for the 9 holes. I am trying to get this to be a running total that updates constantly.
I tried passing the inputs from each textField into an array, and returning the sum of the array to a the textView, but still had issues with the data type from the textField being a string, where as I will only be dealing with integers.
My clunky first pass is as follows -
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var holeOneScore: UITextField!
#IBOutlet weak var holeTwoScore: UITextField!
#IBOutlet weak var holeThreeScore: UITextField!
#IBOutlet weak var holeFourScore: UITextField!
#IBOutlet weak var holeFiveScore: UITextField!
#IBOutlet weak var holeSixScore: UITextField!
#IBOutlet weak var holeSevenScore: UITextField!
#IBOutlet weak var holeEightScore: UITextField!
#IBOutlet weak var holeNineScore: UITextField!
#IBOutlet weak var totalForFrontNine: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//MARK: Calculate Scores
#IBAction func calculate(_ sender: Any) {
let hole1:Int = Int(holeOneScore.text!)!
let hole2:Int = Int(holeTwoScore.text!)!
let hole3:Int = Int(holeThreeScore.text!)!
let hole4:Int = Int(holeFourScore.text!)!
let hole5:Int = Int(holeFiveScore.text!)!
let hole6:Int = Int(holeSixScore.text!)!
let hole7:Int = Int(holeSevenScore.text!)!
let hole8:Int = Int(holeEightScore.text!)!
let hole9:Int = Int(holeNineScore.text!)!
let totalArray = [hole1, hole2, hole3, hole4, hole5, hole6, hole7, hole8, hole9]
let totalScore = totalArray.reduce(0, +)
totalForFrontNine.text = String(totalScore)
print(totalForFrontNine!)
}
It worked, but barely. Any thoughts to modify this or a complete refresh is fine! I am not tied to anything as i am using this project to just teach me the basics.
Thanks in advance, cheers - glassGarrett
Like #jawadali mentioned, use IBOutletCollection. Here is a tutorial about how to use it.
As for the other issue,
...the data type from the textField being a string, where as I will only be dealing with integers.
A quick solution is to set the keyboardType of your textfield to UIKeyboardTypeNumberPad, or .numberPad in Swift.
textField.keyboardType = .numberPad
Or in storyboard,

Adding thousands separators to UILabel numbers

I'm trying to add thousands separators on the numbers exporting out of two UILabels on my code. I've already searched for it on here but none works for my project.
here's the part about calculation in my code so far:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var userCount: UITextField!
#IBOutlet weak var resultLabel: UILabel!
#IBOutlet weak var projectAmount: UITextField!
#IBOutlet weak var totalLabel: UILabel!
#IBAction func polculateButton(_ sender: Any) {
let usersCount = Double(self.userCount.text ?? "") ?? 0
let commissionPercentage = 0.26
let projectsAmount = Double(self.projectAmount.text ?? "") ?? 0
let totalsLabel = (usersCount * projectsAmount)
self.totalLabel.text = "\(totalsLabel)"
let resultsLabel = (usersCount * commissionPercentage * projectsAmount)
self.resultLabel.text = "\(resultsLabel)"
}
}
What code should I add here?

Iphone App Swift main screen works. Aborts on the second page

I am extremely new to Swift. Before I go on to my issue, I would like to thank everyone that takes time out of their day to help solve issues people are having.
I have been experimenting with swift by making a pool calculator app. I have the basic function to figure out how much chlorine someone needs to put in the pool etc...
The main page is just a filler but then when I click "Pool Calculator" , it aborts and gives me the error " SignalBRT"
The code (sloppy as it is) works and i have been experimenting with just a simple addition app that works fine.
But for this one, the code works fine and it gives me an issue. Since it is the "SignalBRT" error, it is such a broad meaning to what the error would be and I do not know what is causing it. I feel like it could be the scrolling view that i added but before i added any functions, it scrolled up and down fine so my assumption would be the "CalculateChlorine" part of the code
Here is my code:
import UIKit
class ViewController: UIViewController {
// #IBOutlet var Number1: UILabel!
//#IBOutlet var Number2: UILabel!
//#IBOutlet var Field1: UITextField!
// #IBOutlet var Field2: UITextField!
#IBOutlet var VolOfpool: UITextField!
#IBOutlet var CurrentR: UITextField!
#IBOutlet var DesiredR: UITextField!
#IBOutlet var labelResult: UILabel!
var DefaultChlorineWeight: Float = 2.0// in oz
var CurrentReading: Float = 1.0
var DesiredReading: Float = 2.0
#IBAction func CalculateChlorine(sender: AnyObject) {
//let a = (Field1.text! as NSString).floatValue
//let b = (Field2.text! as NSString).floatValue
//let sum = a + b
// labelResult.text = "\(sum)"
let Volume = (VolOfpool.text! as NSString).floatValue
let CurrentReading = (CurrentR.text! as NSString).floatValue
let DesiredReading = (DesiredR.text! as NSString).floatValue
let resultForChlorine = (DefaultChlorineWeight * (Volume/10000) * ((DesiredReading - CurrentReading) / 1))
labelResult.text = "\(resultForChlorine)"
}
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.
}
}
here is my error:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate { // it says error SignalBRT here
kind of hard to see without a picture but the first screen of the app has a button called " Pool Calculator" where it sends it to another screen where the user would input the values . The second screen aborts and gives me the error.

Sum of UITextfield values crashes when any field is left blank

Screenshot of simulator screen
Hello all. I just recently started trying to learn coding in Swift 2. I am trying to make a very simple app where I can add scores, push my "calculate" button and display the total in a label. I was able to do that as seen in the screenshot. The problem I have is if I don't put a score in one of the textfields and still try to calculate it, it crashes. This is my code:
class ViewController: UIViewController {
#IBOutlet weak var score1: UITextField!
#IBOutlet weak var score2: UITextField!
#IBOutlet weak var score3: UITextField!
#IBOutlet weak var total: UILabel!
#IBAction func Calculate(sender: AnyObject) {
let score1 = Int(score1.text!)!
let score2 = Int(score2.text!)!
let score3 = Int(score3.text!)!
let alltogether = (scoreone) + (scoretwo) + (scorethree)
total.text = "Your total score is \(alltogether)!"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
Could someone point me in the direction I need to look. I am thinking I need to add some sort of exemption if the field is nil. Or something along the lines of making the default int 0??? Thank you all!
let score1 : Int = Int(score1.text ?? "") ?? 0
let score2 : Int = Int(score2.text ?? "") ?? 0
let score3 : Int = Int(score3.text ?? "") ?? 0
This will yield value 0 in case your fields are empty or otherwise non-numerical (or if UITextField property .text contains nil).

UITextView Font Size

I have a blank/empty UITextView that will output a result based on user-inputs. Everything works great with one small detail that's been bugging me - the font size when it delivers the output is too small for my liking! I tried changing it in the "Attributes Inspector" of storyboard but that only affects it if I decide to have text in the box prior to an output.
My code thus far is
class FreePointViewController: UIViewController {
#IBOutlet weak var fpCoilSizeInput: UITextField!
#IBOutlet weak var fpCoilThicknessInput: UITextField!
#IBOutlet weak var fpLengthInput: UITextField!
#IBOutlet weak var fpPullForceInput: UITextField!
#IBOutlet weak var freePointResult: UITextView!
#IBAction func freePointButton(sender: AnyObject) {
var freePointConst:Double = 20684
calcFreePoint.freePointCoilSize = Double((fpCoilSizeInput.text as NSString).doubleValue)
calcFreePoint.freePointCoilThickness = Double((fpCoilThicknessInput.text as NSString).doubleValue)
calcFreePoint.freePointStretchPipe = Double((fpLengthInput.text as NSString).doubleValue)
calcFreePoint.freePointPullForce = Double((fpPullForceInput.text as NSString).doubleValue)
var freePointArea:Double = M_PI * (calcFreePoint.freePointCoilSize - calcFreePoint.freePointCoilThickness) * calcFreePoint.freePointCoilThickness
var freePoint = (freePointConst * calcFreePoint.freePointStretchPipe * freePointArea) / calcFreePoint.freePointPullForce
var freePointFormat = "0.2"
freePointResult.text = "The pipe is stuck at \(freePoint.format(freePointFormat)) meters"
}
I've tried adding code like [textView setFont:[UIFont boldSystemFontOfSize:15]]; just above my freePointResult.text = ... line but to no avail.
I found the answer to my own question...Check if "Selectable" is checked in the xib/Storyboard attribute inspector. If it's not selectable, the size wont be changed

Resources