Tag: switch statement

Fallthrough in switch statement for iOS/Objective-C

Say I have a switch statement with five cases, but only two real methods ever being called, like so: switch (condition) { case conditionOutcome1: [self firstMethod]; break; case conditionOutcome2: [self secondMethod]; break; case conditionOutcome3: [self firstMethod]; break; case conditionOutcome4: [self firstMethod]; break; case conditionOutcome5: [self secondMethod]; break; default: break; } Is it safe to group […]

switch statement in C / Objective C

I am new to Objetive C, but from all I know about switch statements, the following code should not print anything, because to me it seems that there should be a compilation error. However 2 IS printed. I do not understand why. Could someone please explain? – (void) test { int myVar = 2; switch […]

Is there a way to make Objective-C support a multi-variable switch construct?

I was wondering: is there a way to make Objective-C support a multi-variable switch construct? I mean, very often I have to deal with problems in which the solution depends on a pair of variables instead of a single one. For a long list of single variable values one can use a simple switch/case construct: […]

switch case on NSString in objective c

This question already has an answer here: Using an NSString in a switch statement 9 answers

Why can I not use my constant in the switch – case statement in Objective-C ?

So I have an issue with using a constant variable in the following switch statement in Objective-C. I have Constants.h with the following: // Constants.h extern NSInteger const TXT_NAME; And Constants.m as: // Constants.m #import “Constants.h” NSInteger const TXT_NAME = 1; Then in TabBasic.m I am trying to use this constant in a switch-case statement: […]

Objective-C switch using objects?

I’m doing some Objective-C programming that involves parsing an NSXmlDocument and populating an objects properties from the result. First version looked like this: if([elementName compare:@”companyName”] == 0) [character setCorporationName:currentElementText]; else if([elementName compare:@”corporationID”] == 0) [character setCorporationID:currentElementText]; else if([elementName compare:@”name”] == 0) … But I don’t like the if-else-if-else pattern this produces. Looking at the switch […]

Can Objective-C switch on NSString?

Is there a more intelligent way to rewrite this? if ([cardName isEqualToString:@”Six”]) { [self setValue:6]; } else if ([cardName isEqualToString:@”Seven”]) { [self setValue:7]; } else if ([cardName isEqualToString:@”Eight”]) { [self setValue:8]; } else if ([cardName isEqualToString:@”Nine”]) { [self setValue:9]; }

Using an NSString in a switch statement

Is it possible to use an NSString in a switch statement? Or is it better to just use if / else if?

Weird Switch error in Obj-C

I have this switch statement in my code: switch(buttonIndex){ case 0: [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES]; break; case 1: UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentModalViewController:[imagePicker autorelease] animated:YES]; break; default: [self openEmailViewInViewController:self]; } And at the UIImagePickerController instantiation in Case 1 I am getting an error: error:expected expression before ‘UIImagePickerController’ […]

Can I declare variables inside an Objective-C switch statement?

I think I’m going blind, because I can’t figure out where the syntax error is in this code: if( cell == nil ) { titledCell = [ [ [ TitledCell alloc ] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier ] autorelease ]; switch( cellNumber ) { case 1: NSString *viewDataKey = @”Name”; etc… When I try to compile it, I’m […]