在我的視圖控制器中傳遞小數我有一個uitextfield,用戶可以通過按下添加按鈕來輸入一些費用的小數。 問題:如果像我輸入INT「100」一切正常,但如果我輸入「12.3」的數據是不是即使我轉換文本在雙 注接受:如果我輸入小數,「打印1" 的價值觀和停止,就像沒有數據集 注2:在我的文本字段我設置爲無校正和拼寫檢查由於對文本框的鍵盤xcodebug,但這種變化似乎並不影響本問題swift 3.1無法從UITextField
我聲明:
var amount : Double!
我的按鈕:
@IBAction func addBillButton(_ sender: UIButton) {
print("button pressed 1")
guard self.dataInput.text != nil else {return}
amount = Double(self.dataInput.text!)
guard amount != nil else {return}
print("amount : \(amount!)")
dateShowed = myDatePicker.date as NSDate!
let dateShowedString = convertDateToString(dateToConvert: dateShowed as NSDate)
print("date : \(dateShowedString)")
if presentPickName != nil {
print("name is : \(presentPickName!)")
} else {
print("presentPickName is nil")
}
// guard presentPickData != nil else {return}
if presentPickTag != nil {
print("tag is : \(presentPickTag!)")
} else {
print("presentPickData is nil")
}
//func for saving in core data
// saveExpense(amountToSave: amount, amountDate: dateShowed as NSDate, owner: presentPickName, tag: presentPickTag)
resetViewForNextInput()
self.dataInput.resignFirstResponder()
print("button pressed 2")
}
我的文本框在故事板
編輯解決方案:
在我的按鈕guard let stringFromTxtField = self.dataInput.text else {return}
print(stringFromTxtField)
let myFormatter = NumberFormatter().number(from: stringFromTxtField) as! Double
print(myFormatter)
amount = myFormatter
大說明!但我應該如何在我的代碼中實現它? – biggreentree
@biggreentree正如我寫的那樣使用NumberFormatter – Andrea
我不是那個專家,我在我的更新中添加了一個可能的解決方案,你能檢查我是錯的嗎? – biggreentree