避免在受约束的 CharacterSet 期间从双周期崩溃
Avoid crash from double period during restrained CharacterSet
我有一个即将完善的 GPA 计算器项目。尽管“.0123456789”是唯一允许的字符,但我需要阻止用户输入非浮点数。例如,如果用户输入 2.3。不管出于什么原因,我的程序崩溃了,因为这个 "input" 不是浮点数。我在下面包含了我的代码的基础。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
var allowed = CharacterSet.decimalDigits
let period = CharacterSet.init(charactersIn: ".")
allowed.formUnion(period)
//UNCOMMENT when isSuperset is working
//let characterSet = CharacterSet(charactersIn: string)
//return allowed.isSuperset(of: characterSet)
// Swift 3 appropriate solution
let isSuperset = string.rangeOfCharacter(from: allowed.inverted) == nil
return isSuperset
}
@IBAction func Calculate(_ sender: AnyObject) {
// if not filled in
if Credits1TF.text == "" || Grade1TF.text == "" || CumCredits.text == "" || CumGPA.text == "" || Credits1TF.text == "." || Grade1TF.text == "." || CumCredits.text == "." || CumGPA.text == "."
{
print...
} else
{
//credits
let credit1 = Float(Credits1TF.text!)!
//grades
let grade1 = Float(Grade1TF.text!)!
//math
let firstgrade = credit1 * grade1
我将如何在与文本字段功能相关的文本字段中最实际地添加仅接受一位小数点?或者有 simpler/straightforward 方法吗?
更新 1:
let credit1 = Float(Credits1TF.text!)!
if credit1 != nil
{
let newcredits1 = credit1
//grades
let grade1 = Float(Grade1TF.text!)!
if grade1 != nil
{ let newgrade1 = grade1
除非我处理不当,否则我会收到 "comparing non-optional value of type 'Float' to nil always returns true" 的警告。我的印象是,如果 credits1 或 grade1 != nil,我的数值就可以了,以后不会导致崩溃。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
var allowed = CharacterSet.decimalDigits
let period = CharacterSet.init(charactersIn: ".")
allowed.formUnion(period)
if (textField.text?.contains("."))! && string.contains(".")
{
return false
}
else
{
let isSuperset = string.rangeOfCharacter(from: allowed.inverted) == nil
return isSuperset
}
}
此代码将接受的字符限制为“0-9”,并且还允许输入一个 period/decimal 点。在 if 语句的 else 部分可以看到 0-9 个字符的 return。这个答案的灵感来自 Raj 在上一个问题 How can I limit the number of decimal points in a UITextField?.
中的回答
我有一个即将完善的 GPA 计算器项目。尽管“.0123456789”是唯一允许的字符,但我需要阻止用户输入非浮点数。例如,如果用户输入 2.3。不管出于什么原因,我的程序崩溃了,因为这个 "input" 不是浮点数。我在下面包含了我的代码的基础。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
var allowed = CharacterSet.decimalDigits
let period = CharacterSet.init(charactersIn: ".")
allowed.formUnion(period)
//UNCOMMENT when isSuperset is working
//let characterSet = CharacterSet(charactersIn: string)
//return allowed.isSuperset(of: characterSet)
// Swift 3 appropriate solution
let isSuperset = string.rangeOfCharacter(from: allowed.inverted) == nil
return isSuperset
}
@IBAction func Calculate(_ sender: AnyObject) {
// if not filled in
if Credits1TF.text == "" || Grade1TF.text == "" || CumCredits.text == "" || CumGPA.text == "" || Credits1TF.text == "." || Grade1TF.text == "." || CumCredits.text == "." || CumGPA.text == "."
{
print...
} else
{
//credits
let credit1 = Float(Credits1TF.text!)!
//grades
let grade1 = Float(Grade1TF.text!)!
//math
let firstgrade = credit1 * grade1
我将如何在与文本字段功能相关的文本字段中最实际地添加仅接受一位小数点?或者有 simpler/straightforward 方法吗?
更新 1:
let credit1 = Float(Credits1TF.text!)!
if credit1 != nil
{
let newcredits1 = credit1
//grades
let grade1 = Float(Grade1TF.text!)!
if grade1 != nil
{ let newgrade1 = grade1
除非我处理不当,否则我会收到 "comparing non-optional value of type 'Float' to nil always returns true" 的警告。我的印象是,如果 credits1 或 grade1 != nil,我的数值就可以了,以后不会导致崩溃。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
var allowed = CharacterSet.decimalDigits
let period = CharacterSet.init(charactersIn: ".")
allowed.formUnion(period)
if (textField.text?.contains("."))! && string.contains(".")
{
return false
}
else
{
let isSuperset = string.rangeOfCharacter(from: allowed.inverted) == nil
return isSuperset
}
}
此代码将接受的字符限制为“0-9”,并且还允许输入一个 period/decimal 点。在 if 语句的 else 部分可以看到 0-9 个字符的 return。这个答案的灵感来自 Raj 在上一个问题 How can I limit the number of decimal points in a UITextField?.
中的回答