I’ve confused about memory management of Objective-C.
Example:
.h file
@property(nonatomic,retain) NSString *myString;
.m file
@synthesize myString
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arrayString count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//in this case,i have to check
if(self.myString != nil)
[myString release],self.myString = nil;
//and assign
self.myString = [arrayString objectAtIndex:indexPath.row];
//or i need only assign
self.myString = [arrayString objectAtIndex:indexPath.row];
}
Can anyone explain to me?
Thanks so much.
You don’t need to release, because the property is defined as “retain”, that means it retains the passed object after it releases the previous one.
If, instead of accessing the property you use directly the variable then you need to manually manage the release/retain..
BTW: you can send a message to a nil object…(so without checking it is nil to release it)