2

What is the problem here? Why can't I use 'switch'?

NSString *input;
gets(charInput);
input=[NSString stringWithUTF8String:charInput];
switch (input) { //Statement requires expression of integer type ('NSString *__strong' invalid)
        case "test": //Expression is not an integer constant expression
            NSLog(@"Yes it is");
            break;
        case "Hello": //Expression is not an integer constant expression
            NSLog(@"Hey There!");
        default:
            break;

Thanks!

Yahllilevy
  • 39
  • 1
  • 5
  • Are you looking for an answer about what you can and can't do (because that's not legal Objective-C) or about why the language was written that way (which might be a bit off-topic for SO)? – Simon Aug 12 '15 at 12:42
  • input=[[NSString stringWithUTF8String:charInput]integervalue]; it's solve your problem – Sandy Patel Aug 12 '15 at 12:43
  • It's exactly the same "restriction" as in C, and you should know C pretty well before you attempt to use Objective-C. – Hot Licks Aug 12 '15 at 12:48

2 Answers2

2

In Objective C,

Switch statement can only have int parameter to pass.

That's why you are getting this error.

Hope this helps!

Bharat Nakum
  • 647
  • 6
  • 18
0

This is because, errr, the "switch" statement requires expression of integer type. It only allows you to use numbers.

Actually, this restriction only exists in Objective-C. If you use SWIFT, you can use strings, as shown on this page:

Swift highlights

(Scroll down to the "Switch statements" section.)

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159