元组 Void(又名 ())的值没有成员 "isSuperset"
Value of tuple Void (aka ()) has no member "isSuperset"
我在两个字符集之间创建了一个联合,以便能够使用带有 decimalDigits 字符集的句点。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let allowed = CharacterSet.decimalDigits
let period = CharacterSet.init(charactersIn: ".")
let both = CFCharacterSetUnion(allowed as! CFMutableCharacterSet, period as CFCharacterSet)
let characterSet = NSCharacterSet(charactersIn: string)
return both.isSuperset(of: characterSet as CharacterSet)
}
然而,返回 "both.isSuperset(of: characterSet as CharacterSet)"。如何纠正?
更新一:
与当前建议的答案一起显示的错误
更新 2:
最新更新
尝试这样做:
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
}
I found here 的基础。
更好的是,将“allowed
”(或“both
”,无论您决定如何命名)变成在 viewDidLoad
中创建的 属性,这样您每次键入字符时不要重新创建和合并字符集。
我在两个字符集之间创建了一个联合,以便能够使用带有 decimalDigits 字符集的句点。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let allowed = CharacterSet.decimalDigits
let period = CharacterSet.init(charactersIn: ".")
let both = CFCharacterSetUnion(allowed as! CFMutableCharacterSet, period as CFCharacterSet)
let characterSet = NSCharacterSet(charactersIn: string)
return both.isSuperset(of: characterSet as CharacterSet)
}
然而,返回 "both.isSuperset(of: characterSet as CharacterSet)"。如何纠正?
更新一:
与当前建议的答案一起显示的错误
更新 2:
最新更新
尝试这样做:
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
}
I found here 的基础。
更好的是,将“allowed
”(或“both
”,无论您决定如何命名)变成在 viewDidLoad
中创建的 属性,这样您每次键入字符时不要重新创建和合并字符集。