For example, the NSString * defaultCellIndentifier = @”HelloWorld”;
When should I deallocate that? Are string the only variable in objective-c that can be static?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[BNUtilitiesQuick UtilitiesQuick].currentBusiness=[[BNUtilitiesQuick getBizs] objectAtIndex:[indexPath row]];
//Business * theBiz=[[BNUtilitiesQuick getBizs] objectAtIndex:[indexPath row]];
static NSString * defaultCellIndentifier = @"HelloWorld";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: defaultCellIndentifier];
//UITableViewCell*cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Hello"] autorelease];;
...
return cell;
}
In this case you can’t really deallocate the object, as it is a static string which lives in the read-only section of your program that is mapped into memory. Doing
[@"foo" release]has no effect. You could only assignnilto your variable, but that doesn’t make the string go away.In general, the point of a static variable is to stick around so you don’t want to deallocate it. As long as it’s not a mutable array that only grows and takes up huge portions of memory this isn’t a problem.
Edit to clarify:
Usually, you use a static variable to allocate something that is supposed to be more or less static once. A static variable is shared with all instances, so if you change it, all instances of your class will see this change. Especially with multiple threads this can be a problem, but it’s usually safe to do this:
But once created, you shouldn’t touch the static variable again. If you find the need to change it, you should use an instance variable on your class instead of using a static variable.
Also a warning about multithreading: if you have multiple threads you should make sure that the initalazition of the static variable is done before multiple threads might access it. Because if two threads would each see the uninitialized (nil) variable they would both try to set the new value (race condition) which in theory could even lead to crash. Once the variable is initialized and you only read the variable (and it’s not a mutable object) it’s safe to access the value from different threads.