0

Basically I'm working on something that looks like the phone apps keypad. Everything else is inputting as it should but what I'm trying to figure out is how on this last one to get it when a 1 is inputted first that the number would look like 1 (555) 555-5555. I'm sure my code will need some tweaking to the substringfromindexes to get it to match up, but what I'm trying to get to happen now is just to check if the entered strings first number is a one or not. Because if it is not it will just bypass this part of code.

else if ([self.self.enteredPhoneNumberString length] == 11 && [self.enteredPhoneNumberString characterAtIndex:0] == '1') {
        NSString *firstNum = [self.enteredPhoneNumberString substringToIndex:1];
        NSString *createFirst = [self.enteredPhoneNumberString substringFromIndex:3];
        NSString *firstThree = [createFirst substringToIndex:3];
        NSString *createSecond = [self.enteredPhoneNumberString substringFromIndex:3];
        NSString *secondThree = [createSecond substringToIndex:3];
        NSString *lastSet = [self.enteredPhoneNumberString substringFromIndex:6];
        self.label.text = [NSString stringWithFormat:@"%@ (%@) %@-%@", firstNum, firstThree, secondThree, lastSet];
    }

If you know of the way to adjust the substrinFromIndex to get the effect I'm going for too by all means that would save me time tampering until I find the right order. Thanks in advance for all help. :)

Oh and btw with the above code it did a breakpoint when I got to the this part in the code.

FIX:

Thanks for all the help it works now. Ended up doing this and for some reason the breakpoint doesnt happen when it is done here.

else if ([self.self.enteredPhoneNumberString length] >= 11 ) {
    if ([self.enteredPhoneNumberString characterAtIndex:0] == '1' && [self.self.enteredPhoneNumberString length] == 11) {
        NSString *firstNum = [self.enteredPhoneNumberString substringToIndex:1];
        NSString *createFirst = [self.enteredPhoneNumberString substringFromIndex:1];
        NSString *firstThree = [createFirst substringToIndex:3];
        NSString *createSecond = [self.enteredPhoneNumberString substringFromIndex:4];
        NSString *secondThree = [createSecond substringToIndex:3];
        NSString *lastSet = [self.enteredPhoneNumberString substringFromIndex:7];
        self.label.text = [NSString stringWithFormat:@"%@ (%@) %@-%@", firstNum, firstThree, secondThree, lastSet];
    }
    else{
       self.label.text = [NSString stringWithFormat:@"%@", enteredPhoneNumberString]; 
    }
}

And ya it does seem pointless to have to check if the length is 11 again, but because it was causing issues the previous way I didnt really have much of a choice because I needed it to know whether it exceeded 11 or not so it could start doing that else.

steven
  • 698
  • 3
  • 8
  • 32
  • The debugger points to main.m by default in Xcode 4.2 (for some reason). This question will help you get the debugger pointing to the right location. http://stackoverflow.com/questions/8321184/xcode-always-stopping-at-main-m-after-a-crash Then you will have a better time debugging – Jesse Black Nov 30 '11 at 20:13
  • @Maudicus Ya I found the issue with that was something to do with 4.2 allowing another of the same application to continue running and it wont let you start another. Had to do a full system reboot to fix that. Problem is that I'm still getting break points at your guys solutions :( – steven Nov 30 '11 at 20:17
  • I don't see anything wrong with the code you posted, it looks perfect. Are you sure there isn't a bug somewhere else? – Abhi Beckert Nov 30 '11 at 20:22
  • Did you change self.self to self? Where is it crashing and what is the output? – Jesse Black Nov 30 '11 at 20:29

3 Answers3

2

It could be the if statement above where your code starts is true...

else if ([self.self.enteredPhoneNumberString length] == 11 && [self.enteredPhoneNumberString characterAtIndex:0] == '1') {

needs some cleaning up. You shouldn't check for length 11 here, just check for length > 0 and whether first character is a 1. Do the necessary formatting, trim white space and (), then check the length. Also, self.self is a potential problem, drop the second self

Please disregard CocoaFu's comment his solution is better, but the comment is misleading.

In the following code, my log statement shows up in the console

NSString * test = [NSString stringWithString:@"test"];
unichar c = [adjusted characterAtIndex:0];

if (c == 't')   {
    NSLog(@"can compare char");
}
Jesse Black
  • 7,966
  • 3
  • 34
  • 45
  • So I tried this else if ([self.enteredPhoneNumberString characterAtIndex:0] == '1') { – steven Nov 30 '11 at 19:31
  • The comparison using `==` is not comparing the strings, it is comparing the pointers to the strings. – zaph Nov 30 '11 at 19:42
  • 1
    He needs to do a string length check, because if the length is `0` the `characterAtIndex:` call will throw an exception. @CocoaFu he is not comparing strings, he is comparing characters. The `==` operator is correct. – Abhi Beckert Nov 30 '11 at 20:19
  • @AbhiBeckert that is correct. It is possible that there is a if statement above the code we see to make sure the string isn't blank, but not guaranteed. I was a little caught up on the fact that his example string has 15 characters – Jesse Black Nov 30 '11 at 20:27
  • @Maudicus I can't explain why it worked really, but basically I had the same code. The outside part of the other else if's is still checking the length. On the inside I put another if for checking if the characterAtIndex:0 was 1. It works, but I'm not sure why the previous one didn't just work in that case – steven Nov 30 '11 at 20:30
  • @steven Since we are talking about not being able to explain why something works. Check out my question (Android) that I mysteriously resolved, with little comprehension to why it works. It is usually best to understand how you get past a bug, but sometimes it is ok to just get past it. http://stackoverflow.com/questions/8236278/android-why-would-an-image-in-activity-1-end-up-in-activity-2-zxing-captureac – Jesse Black Nov 30 '11 at 21:03
0
if [[self.enteredPhoneNumberString substringToIndex:1] isEqualToString:@"1"] {

}

I think this is what you are asking for...

Owen Hartnett
  • 5,925
  • 2
  • 19
  • 35
  • You'll want to protect against the case where `self.enteredPhoneNumberString` is empty -- `-substringToIndex:` will oh so helpfully throw an exception in this case. :/ – Josh Bleecher Snyder Nov 30 '11 at 19:21
  • @Owen Hartnett I gave that a try and it stopped on it as a breakpoint again. Could it be something with the other code. Even though it seems to work fine in the other sections of the else if's. I only added the top two of it – steven Nov 30 '11 at 19:32
  • 1
    `substringToIndex` followed by `isEqualToString` will give exactly the same result as @steven's `characterAtIndex` code, but comparing with `characterAtIndex` is a million times faster. I wouldn't use `subStringToIndex` or `isEqualToString` for something so simple. – Abhi Beckert Nov 30 '11 at 20:23
0

The easiest compare of the first character:

[self.enteredPhoneNumberString hasPrefix:@"1"]
zaph
  • 111,848
  • 21
  • 189
  • 228
  • @beryllium Ok, do you have any idea why there might be a SIGBART in the section above? – steven Nov 30 '11 at 19:50
  • Well, one note `characterAtIndex` returns unichar (not a NSString) and '1' is also not a NSString – beryllium Nov 30 '11 at 19:57
  • @CocoaFu You can compare characters like that. '1' is a character and the return type of characterAtIndex is unichar... however hasPrefix is a better implementation – Jesse Black Nov 30 '11 at 20:00
  • @CocoaFu Just like the other solutions it continues to give me a break point there. Thing is if I take that part out and run the next else if it works just fine – steven Nov 30 '11 at 20:18