Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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?
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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?

Exactly. The switch statement only takes integer constant values.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
Perhaps Objective - C is not as cool as Pascal still is, with it's limited abilities :)

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.
 

mfram

Contributor
Jan 23, 2010
1,307
343
San Diego, CA USA
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;
    }
}
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
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
 

wlh99

macrumors 6502
Feb 7, 2008
272
0
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

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;
    
    
}
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Thanks for the ideas!

It's not every number, it's these letters with these values
A = 10
B = 20
C = 30
D = 50
E = 70
F = 80
G = 90
H = 100
I = 120
J = 140
K = 150
L = 160
M = 170

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!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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!

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
 
Last edited:

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
You could try:

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.