Declare instance in "switch" or "if else"

Discussion in 'Mac Programming' started by ncohen, May 20, 2009.

  1. ncohen macrumors newbie

    Joined:
    Mar 31, 2009
    #1
    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
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. Scratch75 macrumors newbie

    Joined:
    Aug 30, 2007
    #3
    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.
     
  4. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #4
    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] );
    }
     
  5. autorelease macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #5
  6. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    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];
    		}
    	}
    
    
     

Share This Page