As you can see from the output, the switch statement completely skips over case 'view1'. And I'm having trouble understanding what the warnings mean.
Asked
Active
Viewed 82 times
-2
-
You're passing a single char into the function, it can never be the same as a multi character string. – Joachim Isaksson Mar 09 '14 at 11:02
-
@JoachimIsaksson oh, can it be changed to multi-char? – louisinhongkong Mar 09 '14 at 11:05
-
1I suggest using `NSString*` as a parameter type (constants are then declared as `@"view1"`). Even then, I don't think there's a possibility to use `switch` to compare them. – Joachim Isaksson Mar 09 '14 at 11:07
-
1I think if-else would be better for string comparisons – mcy Mar 09 '14 at 11:09
-
@JoachimIsaksson yeh I tried using NSString instead but it doesn't work with switch statements :( – louisinhongkong Mar 09 '14 at 11:09
-
what kind of data type is 'view1' in your example? – MrBr Mar 09 '14 at 11:10
-
use `if ([view isEqualToString:@"view1"]) {}` – MrBr Mar 09 '14 at 11:11
-
1You can't have multiple characters between single quotes. Single quotes denote a char, which is always one character, not a string. Furthermore, strings in switch statements are generally not a good idea (you'll be comparing references instead of values). – 11684 Mar 09 '14 at 11:11
-
2Some compilers (including gcc and clang) permit up to *four* characters in a constant: http://stackoverflow.com/questions/6944730/multiple-characters-in-a-character-constant. – Martin R Mar 09 '14 at 11:16
1 Answers
3
Try to change the method signature to
- (void)switchViewTo:(NSString *)view
{
if ([view isEqualToString:@"view1"]) {
NSLog(@"view 1");
} else if ([view isEqualToString:@"view2"]) {
NSLog(@"view 2");
} else {
NSLog(@"whatever");
}
}
In the designated initializer you call [self switchToView:@"view1"];

MrBr
- 1,884
- 2
- 24
- 38