Limiting NSString to numbers as well as # if chars

2019-04-13 01:45发布

I have searched the site for my answer, however I think it is also how I got myself into this problem.

I'm trying to limit my 1 and only text field to 10 characters and only numbers.

Below is code mostly off other questions on this site. What I am trying to do is mash this code together for my limits

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEGAL] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    NSString *newString = [myTextField.text stringByReplacingCharactersInRange:range withString:filtered]; 
    return !([newString length] > 10);
}

There are no problems with my build until I click my textfield... but I get a green arrow warning stating " thread stopped at breakpoint 1 " I've tried a few different things now, but now feel I am stumped, I'm hoping this is something real easy for someone here...

they will work individually (one or the other) with the code below, but I can't seem to get them working together.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *newString = [myTextField.text stringByReplacingCharactersInRange:range withString:string];
    return !([newString length] > 10);
}

This will limit my input to 10 and ...

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEGAL] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}

This also restricts my input to numbers only with no problems. provided I defined LEGAL as only numbers...

I just can't seem to get them to work together... Can anyone help me with this one ???

1条回答
看我几分像从前
2楼-- · 2019-04-13 02:09

There are many ways to do this, but I think this might be the most understandable:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    // Check for non-numeric characters
    NSUInteger lengthOfString = string.length;
    for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {
        unichar character = [string characterAtIndex:loopIndex];
        if (character < 48) return NO; // 48 unichar for 0
        if (character > 57) return NO; // 57 unichar for 9
    }
    // Check for total length
    NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
    if (proposedNewLength > 10) return NO;
    return YES;
}

The first section essentially check a non-empty string for characters that are not between '0' and '9' if it finds one anywhere in the new string it rejects the edit. The second determines what the length of the text will be if the editing is completed and compares to 10 if it's to high it rejects the edit. If the function passes all those tests then the edit is allowed.

There are no problems with my build until I click my textfield... but I get a green arrow warning stating " thread stopped at breakpoint 1 "

You have apparently set a breakpoint in your textField:shouldChangeCharactersInRange:replacementString: method. If your not familiar with breakpoints they look like this: (The blue arrow on the row numbers column) And you can toggle whether or not they are enabled with the button pictured on the right:

enter image description here enter image description here

Breakpoints are very handy, and I suggest you learn to use them. But for now simply click on the breakpoint and drag it from the column, like an icon from the dock, or toggle that option off.

查看更多
登录 后发表回答