5

Want to get index of the last occurrence of # but bellow code working fine for the other character but not giving perfect result for particularity "#"

Code Working fine in viewDidLoad but not working in textfield shouldChangeCharactersInRange.

Code:

txtTest.text = @"@ashish @test #vijay $4030 @post";


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
   NSRange range = [textField.text rangeOfString:@"a" options:NSBackwardsSearch];        
    NSLog(@"a :: %d   ",range.location);

    range = [str rangeOfString:@"@" options:NSBackwardsSearch];
    NSLog(@"@ :: %d   ",range.location);

    range = [str rangeOfString:@"#" options:NSBackwardsSearch];
    NSLog(@"# :: %d",range.location);

    range = [str rangeOfString:@"$" options:NSBackwardsSearch];
    NSLog(@"$ :: %d\n",range.location);

}

Result :: a :: 17 @ :: 26 # :: 2147483647 $ :: 20

enter image description here

5
  • 2
    That is NSNotFound. Maybe the characters looks the same but are not, or there is a zero-width space in there or some such?
    – Gerd K
    Commented Feb 29, 2016 at 7:12
  • Yep, there is an invisible character in your @"#" string. Use the arrow keys to move the cursor across it and you'll see it.
    – Gerd K
    Commented Feb 29, 2016 at 7:15
  • Copied and Pasted to Xcode (this may autoformat some things, maybe even encoding stuff). Fixed NSString typo on first line, runs fine.
    – lupz
    Commented Feb 29, 2016 at 7:17
  • a :: 17 @ :: 26 # :: 13 $ :: 20, I found this result correcting NSString spelling
    – Jamil
    Commented Feb 29, 2016 at 7:17
  • @jamil When i put same code in viewDidLoad then its work fine but in shouldChangeCharactersInRange its not working.. replace "str" with string Commented Feb 29, 2016 at 7:24

2 Answers 2

8

Try %lu unsigned long instead of %d . Its working fine

NSString *str = @"ashish @test #vijay $4030 @post";

NSRange range = [str rangeOfString:@"a" options:NSBackwardsSearch];
NSLog(@"a :: %lu   ",(unsigned long)range.location);

range = [str rangeOfString:@"@" options:NSBackwardsSearch];
NSLog(@"@ :: %lu   ",(unsigned long)range.location);

range = [str rangeOfString:@"#" options:NSBackwardsSearch];
NSLog(@"# :: %lu",(unsigned long)range.location);

range = [str rangeOfString:@"$" options:NSBackwardsSearch];
NSLog(@"$ :: %lu\n",(unsigned long)range.location);

a :: 17

@ :: 26

# :: 13

$ :: 20

6
  • Thanks for the response and in viewDidLoad its work fine but in shouldChangeCharactersInRange its not working Commented Feb 29, 2016 at 7:32
  • Working fine in shouldChangeCharactersInRange too . Commented Feb 29, 2016 at 7:42
  • Strange but its not working in my machine :( i had also attached screenshot Commented Feb 29, 2016 at 7:46
  • 1
    it is showing me this random big integer in case if the charcter isn't in text field as text "ash" the logs showing a :: 0 @ :: 9223372036854775807 # :: 9223372036854775807 $ :: 9223372036854775807 . So double check your delegated and fields. Commented Feb 29, 2016 at 7:55
  • and please try to change the name of your range object , because same name NSRange object is from shouldChangeCharactersInRange method. Commented Feb 29, 2016 at 7:57
3

First, you have a spelling mistake.It is NSString *str, not NSSting *str.

The rest works well on my machine.

NSString *str = @"ashish @test #vijay $4030 @post";

NSRange range = [str rangeOfString:@"a" options:NSBackwardsSearch];
NSLog(@"a :: %lu   ",range.location);

range = [str rangeOfString:@"@" options:NSBackwardsSearch];
NSLog(@"@ :: %lu   ",range.location);

range = [str rangeOfString:@"#" options:NSBackwardsSearch];
NSLog(@"# :: %lu",range.location);

range = [str rangeOfString:@"$" options:NSBackwardsSearch];
NSLog(@"$ :: %lu\n",range.location);

answer:

2016-02-29 15:19:39.554 StackOverflowDemo[9189:1825956] a :: 17   
2016-02-29 15:19:39.555 StackOverflowDemo[9189:1825956] @ :: 26   
2016-02-29 15:19:39.555 StackOverflowDemo[9189:1825956] # :: 13
2016-02-29 15:19:39.555 StackOverflowDemo[9189:1825956] $ :: 20

Can you say more clearly?

2
  • in viewDidLoad its work fine but in shouldChangeCharactersInRange its not working :( Commented Feb 29, 2016 at 7:36
  • Did you check (and re-enter) the string as was mentioned in the comments? You do have some "non-standard" character in there, and the different locations clearly indicate that as well.
    – Eiko
    Commented Feb 29, 2016 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.