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

bluehill

macrumors newbie
Original poster
Feb 5, 2011
22
0
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
 
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.
 
Last edited:

the link shows example of int what if we use objects like NSString or NSData

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.

even am facing the same problem, i thought their may be some workaround for this...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.