- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField.tag==1) {
if ([string isEqualToString:@" "]) {
if (!textField.text.length) {
return NO;
}
if ([[textField.text stringByReplacingCharactersInRange:range withString:string] rangeOfString:@" "].length) {
return NO;
}
}
if ([textField.text stringByReplacingCharactersInRange:range withString:string].length < textField.text.length) {
return YES;
}
}
return YES;
}
在swift 3中我几乎已经转换了但是“rangeOfString”是可以访问的 任何人都可以将此Objective c转换为swift 3
答案 0 :(得分:1)
Swift 3.0
将Objective c转换为Swift 3。祝你好运!
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if(textField.tag == 1) {
let nsString = textField.text as NSString?
var newString = nsString?.replacingCharacters(in: range, with: string)
if(string == " "){
if(textField.text!.characters.count < 1){
return false;
}
if (newString != nil && newString!.range(of: " ") != nil){
return false;
}
}
if(newString != nil && newString!.characters.count < textField.text!.characters.count){
return true
}
}
return true
}