Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ncohen

macrumors newbie
Original poster
Mar 31, 2009
8
0
Hello,
I'm trying to declare an instance in a switch... but it doesn't work! I get an error.

Code:
	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
 
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
 
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.
 
Code:
    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:
Code:
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] );
}
 
Hello,
I'm trying to declare an instance in a switch... but it doesn't work! I get an error.

Code:
	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:

Code:
	switch ( [sender tag] ) {
		case 1:
		{
			StoryView* sub = [[StoryView alloc] init];
		}
			break;
		case 2:
		{
			ImageView* subMenu = [[NSView alloc] init];
		}
	}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.