Sorry to ask this (I rarely use switch statements) but I am getting an error with this but it seems valid to me (but obviously isn't):
NSInteger section=indexPath.section;
switch(section){
case 0:
Item *mi = self.miArray[indexPath.row]; // <- expected expression
...
return cell;
break;
case 1:
Item *mi = self.miArray[indexPath.row];
break;
}
What am I doing wrong?
You can either put the case in braces (case 0: { Item *mi ... }) or add a ; after the case statement (case 0:;).
Either of that should help but I actually forgot why this is necessary.
Found an explanation here: Weird Switch error in Obj-C
Declaration is not allowed within switch.
Declare Item before entering to switch and do the initialization within the switch.
Related
I'm trying to use a variable as the case match, however I get "Expression is not an integer in Objective-C.
Is it possible to use variable in switches in this manner?
int count = [array count];
switch ([number to check]) {
case 0:
//first statement
break;
case 1 ... (count -1):
//somewhere between 1 and the next to last statement
//Basically the middle
break;
case count:
//Last statement
default:
break;
}
Objective-C (and C) switch only supports a single primitive constant value for each case statement (or a range as pointed out in the answer by TwoStraws). You would be much better off writing your code using if/else:
if ([number to check] == 0) {
} else if ([number to check] >= 1 && [number to check] < count) {
} else if ([number to check] == count) {
} else {
}
Objective-C's switch statement does support ranges of values as you've seen, but doesn't support variable matches I'm afraid.
So, the below code is valid because I've used exact integers:
int numberOfKittens = 12;
NSString *kittenDescription;
switch (numberOfKittens) {
case 0 ... 5:
kittenDescription = #"Need more kittens";
break;
case 6 ... 10:
kittenDescription = #"That's a good number of kittens.";
break;
case 11 ... 20:
kittenDescription = #"Are you sure you can handle that many kittens?";
break;
default:
kittenDescription = #"You must really love kittens!";
}
…but trying to put a variable in place of any of those will fail.
If this is something you desperately want, consider using Swift because it has a much more expressive switch matching system. Here's that same code in Swift, now with a variable being used to match a case:
let upperLimit = 20
let numberOfKittens = 19
var kittenDescription = ""
switch (numberOfKittens) {
case 0 ... 5:
kittenDescription = "Need more kittens"
case 6 ... 10:
kittenDescription = "That's a good number of kittens."
case 11 ... upperLimit:
kittenDescription = "Are you sure you can handle that many kittens?"
default:
kittenDescription = "You must really love kittens!"
}
I haven't used the ternary operator much and I'm getting an error on this:
(isInitializing) ? (return YES) : (isInitializing = 1);
Error is: "Expected Expression" and it's pointing to return YES.
Don't use the Ternary Operator to "do stuff" but to return one of two values.
So this is a valid case:
NSString *something = (isInitializing ? #"value a" : #"value b");
In your case, you might want to do this instead:
if (isInitializing) {
return YES;
} else {
isInitializing = 1;
}
The ternary operator is used to return one of two values depending on a condition. It is not so much used to execute statements, hence the return is a bit of a problem. I would rather use an if when you do not want to distinguish values, but rather have two different execution paths.
I have this code who try to compare strings in Switch Case:
char input[50+1];
fgets( input, 50, stdin );
switch (input) {
case "register": NSLog(#"Voce escolheu a opcao de cadastro");
break;
case "enter": NSLog(#"Voce escolheu a opcao de entrada");
break;
case "exit": NSLog(#"Voce escolheu a opcao de saida");
break;
}
This command returns me an error, because I believe that we can not write a text after the 'case' command. I would have someone could help me solve this problem, I believe there are other ways to make a Switch Case using strings, but how?
The lookup option works pretty well. Consider:
NSArray *strings = #{#"string1", #"string2"};
NSUInteger index = [strings indexOfObject:input];
switch(index) {
case 0:
//stuff for string 1;
case 1:
// stuff for string 2:
case NSNotFound:
// not found;
}
You can't. Switch only works with integers. The best options are a chain of if-else statements or a lookup table (e.g. an NSDictionary).
I am trying to create a class similar to the built in NSDictionary class, but I want to add some extra functionality and make it easier to use. In the .m file I have the following piece of code:
-(void)newEntryWithKey:(NSString *)theKey andValue:(NSString *)theValue{
if (![theKey isEqual:#""]) && (![theValue isEqual:#""]){
[self.keys addObject:theKey];
[self.values addObject:theValue];
self.upperBound++;
}else{
return
}
}
It gives me an "Expected Identifier" error at the start of the second portion of the if statement after the "&&". Would someone be able to help me with this?
EDIT: The original problem is fixed but now there is a new error at the end of the if statement.
-(void)newEntryWithKey:(NSString *)theKey andValue:(NSString *)theValue{
if (theKey.length && theValue.length) {
[self.keys addObject:theKey];
[self.values addObject:theValue];
self.upperBound++;
}else{
return
} //<-- error here "Expected expression"
}
It should be:
if (![theKey isEqual:#""] && ![theValue isEqual:#""]) {
Though a better check for non-empty strings would be:
if (theKey.length && theValue.length) {
Your original if statement will give incorrect results if either theKey or theValue is nil. My 2nd option works in either case.
Update:
The problem with the updated code is the missing ; after the return statement.
Usually after if you should have one condition in parentheses. You're missing parentheses.
I just Don't understand how to use a boolean operator inside a switch statement
switch (expression) {
case > 20:
statements
break;
case < -20:
statements
break;
}
Edit:
I don't want an If () statement.
You can't. Use if() ... else ....
The nearest thing available to what you want uses a GCC extension and is thus non-standard. You can define ranges in case statements instead of just a value:
switch(foo)
{
case 0 ... 20: // matches when foo is inclusively comprised within 0 and 20
// do cool stuff
break;
}
However, you can't use that to match anything under a certain value. It has to be in a precise range. Switches can only be used to replace the comparison operator against a constant, and can't be used for anything more than that.
switch ((expression) > 20) {
case true:
statements
break;
case false:
default:
statements
break;
}
What.. you want more than 1 boolean in a case? You could do this
int ii = ((expression) > 20) + 2 * ((expression) < -20);
switch (ii) {
case 1:
statements
break;
case 2:
statements
break;
}
This, IMO is pretty bad code, but it is what you asked for...
Just use the if statement, you'll be better off in the long run.