View Full Version : Declare instance in "switch" or "if else"
ncohen
May 20, 2009, 04:55 PM
Hello,
I'm trying to declare an instance in a switch... but it doesn't work! I get an error.
switch ([sender tag]) {
case 1:
StoryView *sub = [[StoryView alloc] init];
break;
case 2:
ImageView *subMenu = [[ImageView alloc] init];
}
Does anyone know why? and is there any alternative to do that (I tried to do with if else but same problem)? I want to save memory it's why I don't want to declare all of them (I have 9 instances).
Thanks
lee1210
May 20, 2009, 05:05 PM
You'll need to explain what you're trying to do a bit more clearly to get a more exact answer (also, always include the errors you get, don't just say you get them).
Also, what sort of device are you targeting? Declaring 9 pointers inside the switch's {,but before your cases will use 8*sizeof(void *) bytes more than if you could declare one per case... and if 32 or 64 bytes of memory are that big of a deal, i'd think you'd be using primitives and not objects.
You could go about it a bit differently and declare one NSObject * or id inside the switch, then use that in each case.
-Lee
Scratch75
May 20, 2009, 05:20 PM
Not knowing what 'sender' is, I'm going to assume it's of type 'id'. If that's the case, the compiler has no way of knowing what data type the 'tag' message is going to return, assumes it's a generic NSObject* (id) and complains because it expects an integer (not an NSObject*) inside a switch statement.
Your best bet would be to cast 'sender' to its actual runtime type before calling 'tag', that way the compiler will know that 'tag' returns an integer.
Guiyon
May 20, 2009, 05:38 PM
switch ([sender tag]) {
case 1:
StoryView *sub = [[StoryView alloc] init];
break;
case 2:
ImageView *subMenu = [[ImageView alloc] init];
}
IIRC, that is not valid code. Additionally, once you leave the switch statement both of those vars go out of scope and you leak memory.
You would need to do something like:
StoryView* sub = nil;
ImageView* subMenu = nil;
switch( [sender tag] ) {
case 1:
sub = [[StoryView alloc] init];
break;
case 2:
subMenu = [[ImageView alloc] init];
break;
default:
NSLog( @"Unknown value %d", [sender tag] );
}
autorelease
May 20, 2009, 07:59 PM
You can't declare variables after a case label. It's not valid C.
This was fully explained here: http://forums.macrumors.com/showthread.php?t=680095
lloyddean
May 20, 2009, 09:24 PM
Hello,
I'm trying to declare an instance in a switch... but it doesn't work! I get an error.
switch ([sender tag]) {
case 1:
StoryView *sub = [[StoryView alloc] init];
break;
case 2:
ImageView *subMenu = [[ImageView alloc] init];
}
Does anyone know why? and is there any alternative to do that (I tried to do with if else but same problem)? I want to save memory it's why I don't want to declare all of them (I have 9 instances).
Thanks
In "Project Settings" setting "C Language Dialect" to "GNU99" will
allow the following to compile:
switch ( [sender tag] ) {
case 1:
{
StoryView* sub = [[StoryView alloc] init];
}
break;
case 2:
{
ImageView* subMenu = [[NSView alloc] init];
}
}
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.