create static NsMutableData? alloc and dealloc

Discussion in 'iOS Programming' started by bluehill, Mar 8, 2011.

  1. bluehill macrumors newbie

    Joined:
    Feb 5, 2011
    #1
    I want to create a static NsMutableData with its own getter setter property,
    Where should i initializ and alloct the variable and where should i release it..

    Code:
    static NSMutableData  *updateData;
    @implementation DbUpdates
    
    
    +(void)initialize{
    	NSLog(@"updatedate initialized");
    	updateData = [[NSMutableData alloc] init];   
    }
    
    +(NSMutableData*)getUpdateDate{
    	return updateData;
    }
    
    +(void)setUpdateDate:(NSData*)data{
    	[updateData appendData:data];
    /*
    [data retain];
    [updateData appendData:data];
    */
    }
    
    }
    
    when i access the updateData(which consists of xml data) it throws error when retrieving foundCharacters
     
  2. ulbador, Mar 8, 2011
    Last edited: Mar 8, 2011

    ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #2
    This is a good example of how that would work:

    http://jongampark.wordpress.com/2009/04/25/class-variable-for-objective-c-and-c/

    The tl;dr version of this is you just basically check if it's already initialized before you try and do it again on this line:

    Code:
    if (updateData == nil)
    updateData = [[NSMutableData alloc] init];
    
    Something like that.

    Then I assume you would just release it in the [dealloc] as normal (probably checking if it has already been done or not before you try). Though I imagine that would create a problem if you released it in one instance of the class and then tried to access it again in the second instance. I guess that's part of the reason I never really used statics in Java for anything but constants. I've definitely had some headaches from it before.

    Statics work a little different in Objective C vs Java and you may want to check into those differences. You may be better off using constants or externs depending on what you are trying to accomplish.

    I think a big difference (and I could be wrong), but you must then interact with the static variable via methods, not directly. I've only ever read about it, never actually had a reason to do it.
     
  3. bluehill thread starter macrumors newbie

    Joined:
    Feb 5, 2011
    #3
    the link shows example of int what if we use objects like NSString or NSData

    even am facing the same problem, i thought their may be some workaround for this...
     

Share This Page