Reputation: 1
I'm trying to make an app that prorates rent for class, but whenever I plugin all of the data and press the calculate button, it just show the word "inf" in the text box.
the code is as follows:
class ViewController: UIViewController {
@IBAction func tapIt(sender: UITapGestureRecognizer) {
monthlyRent.resignFirstResponder()
moveInMonth.resignFirstResponder()
moveInDay.resignFirstResponder()
moveInYear.resignFirstResponder()
billOn.resignFirstResponder()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
var daysInMonth : Int = Int()
var temp : Int = Int()
var daysLivingThere : Int = Int()
var rentPerDay : Double = Double()
var billingDays2 : Int = Int()
var proratedRent : Double = Double()
var addIn = 1
var passingData : String = String()
var dollar = "$"
@IBOutlet var monthlyRent: UITextField!
@IBOutlet var moveInMonth: UITextField!
@IBOutlet var moveInDay: UITextField!
@IBOutlet var moveInYear: UITextField!
@IBOutlet var billOn: UITextField!
@IBOutlet var fieldA: UITextField!
@IBAction func calculate(sender: UIButton) {
let rentMonthly : Double? = Double(monthlyRent.text!)
let dayMoveIn : Int? = Int(moveInDay.text!)
let billingDays : Int? = Int(billOn.text!)
let monthDays : Double? = Double(daysInMonth)
switch moveInMonth
{
case 1, 3, 5, 7, 8, 10, 12:
daysInMonth = 31
case 4, 6, 9, 11:
daysInMonth = 30
case 2:
daysInMonth = 28
default:
break
}
billingDays2 = billingDays! - addIn
temp = daysInMonth - dayMoveIn!
daysLivingThere = temp + billingDays2
rentPerDay = rentMonthly! / monthDays!
let a : Double? = Double(daysLivingThere)
proratedRent = rentPerDay + a!
let b : String? = String(proratedRent)
fieldA.text = dollar + b!
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "btnSubmitSegue") {
let svc = segue.destinationViewController as! SecondViewController;
svc.dataPassed = fieldA.text!
}
}
}
All of the code is located inside of a button. I want to transfer the data from the text box to a secondary view controller and desperately need the text box to work.
Upvotes: 0
Views: 172
Reputation: 1431
David is right:
change this:
let rentMonthly : Double? = Double(monthlyRent.text!)
let dayMoveIn : Int? = Int(moveInDay.text!)
let billingDays : Int? = Int(billOn.text!)
let monthDays : Double? = Double(daysInMonth)
switch moveInMonth
{
case 1, 3, 5, 7, 8, 10, 12:
daysInMonth = 31
case 4, 6, 9, 11:
daysInMonth = 30
case 2:
daysInMonth = 28
default:
break
}
to this:
let rentMonthly : Double? = Double(monthlyRent.text!)
let dayMoveIn : Int? = Int(moveInDay.text!)
let billingDays : Int? = Int(billOn.text!)
switch moveInMonth
{
case 1, 3, 5, 7, 8, 10, 12:
daysInMonth = 31
case 4, 6, 9, 11:
daysInMonth = 30
case 2:
daysInMonth = 28
default:
break
}
let monthDays : Double? = Double(daysInMonth)
Upvotes: 0
Reputation: 143
To add on to what David Berry said, I ran this in a playground and it failed to compile because "error: constant 'daysInMonth' used before being initialized". Make sure to initialize these values and you should be good.
Upvotes: 0
Reputation: 41246
Probably because monthDays
is set before daysInMonth
, which it depends on, move the let monthDays :...
line to after the moveInMonth
switch.
Upvotes: 1