-1

我对 Objective-C 的内存管理感到困惑。例子:

.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];
}

谁能给我解释一下?非常感谢。

4

3 回答 3

2

您只需要分配字符串 .. 并nildeallocandviewDidUnload方法中释放/分配它。

于 2012-07-05T15:04:30.017 回答
0
 if(self.myString != nil){
        [myString release],self.myString = nil;
 }

This is enough:

  if(self.myString != nil){
            self.myString = nil;
     }

As for your question, you only need this:

  self.myString = [arrayString objectAtIndex:indexPath.row];
于 2012-07-05T15:06:11.483 回答
0

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)

于 2012-07-05T15:06:20.113 回答