Switch Statement Obj-C

Discussion in 'Mac Programming' started by larswik, Aug 14, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    Hello. I have a method and I want to pass in a NSString which contains just a single char from an NSTextField. The return value will be an int.

    If I pass in value is @"a" the return value would be 1 as an example.

    Code:
    -(int)findCritValue: (NSString *) letter{
        int i;
        switch (letter) {
            case @"a":
                i = 1;
                return i;
                break;
                
            default:
                break;
        }
    }
    
    I am getting an error saying the Switch only take integer values?
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Exactly. The switch statement only takes integer constant values.
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    Switch being restricted to integer constants can be a PITA. It is a shame that Objective-C didn't extend C's switch to cover NSString, even if it compiles down to a cascade of if/else if/.../else.
     
  5. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #5
    How about something like:

    Code:
    -(int)findCritValue: (NSString *) letter{
        int i;
        unichar u = [letter characterAtIndex:0];
        switch (u) {
            case 'a':
                i = 1;
                return i;
                break;
                
            default:
                break;
        }
    }
     
  6. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    larswik, I think it works fine with char because char is just an unsigned 8 bit integer.

    'a' is just convenient shorthand for 0x61 or decimal 97.

    NSString isn't so simple. Maybe you can convert your NSString to char * in ASCII or UTF-8.

    B
     
  7. wlh99 macrumors 6502

    Joined:
    Feb 7, 2008
    #7
    Also,
    Instead of 26 switch statements (assuming one for each letter) you can subtract 96 from the char for a lowercase letter, and 64 for a lowercase letter. You would also want some error checking to ensure the returned value makes sense.

    Code:
    -(int)findCritValue: (NSString *) letter{
        int i;
        unichar u = [letter characterAtIndex:0];
        if (u > 96) i = u-96;
        else i = u-64;
        if ((i < 1) || (i > 26)) {
            NSLog(@"Error - non letter entered");
            
            return 0;
        }
        return i;
        
        
    }
    
     
  8. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    Thanks for the ideas!

    It's not every number, it's these letters with these values
    I think it will be easier to to use a bunch of If statements in my method and adjust the passed in argument to upper or lower case. I was a little surprised that I could not compare strings in the Switch like the 'isEqualTo' or something.

    Thanks!
     
  9. lee1210, Aug 14, 2011
    Last edited: Aug 14, 2011

    lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    You could try:
    Code:
    NSDictionary *valueMap = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt: 10], @"A",
    [NSNumber numberWithInt: 20], @"B",
    [NSNumber numberWithInt: 30], @"C",...];
    
    Then

    Code:
    NSNumber *myValue = [valueMap valueForKey:myKey];
    
    If myValue is null, didn't match, no good. Otherwise you can pass intValue to get your int out.

    -Lee
     
  10. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #10
    What a simple Cocoa way of approaching this problem.

    Of course a dictionary has keys and values.

    Yet another reason I find it harder and harder to recommend learning C before Objective-C/Cocoa if that is your goal. When you do you tend to miss the forest for all the trees you are used to dealing with in lower level languages.

    B
     

Share This Page