我试图制作一个按类比例分配租金的应用,但每当我插入所有数据并按下计算按钮时,它只会显示“" inf"在文本框中。
代码如下:
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!
}
}
}
所有代码都位于按钮内。我想将数据从文本框传输到辅助视图控制器,并且迫切需要文本框才能工作。
答案 0 :(得分:1)
可能是因为monthDays
设置在daysInMonth
之前,它依赖于let monthDays :...
,将moveInMonth
行移到var selectedOptions = $('#cols option:selected').text();
切换之后。
答案 1 :(得分:0)
再加上David Berry所说的,我在游乐场中运行它并且无法编译,因为“错误:在初始化之前使用常量'daysInMonth'”。确保初始化这些值,你应该很好。
答案 2 :(得分:0)
大卫是对的:
改变这个:
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
}
到此:
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)