0

我将 ARC 与 xCode 4.6 一起使用,并且在尝试访问.notesan 的属性EKEvent以分配给cellForRowAtIndexPath.

有趣的是,直到我在任何不cellForRowAtIndexPath调用EKEvents.

根据 Instruments 的说法,泄漏的发生是因为这条线:

eventNotes = [NSString stringWithFormat:@"%@", [currentEvent notes]];

在此处输入图像描述

eventNotesivar我在我的 .h 文件中声明的。

我已经尝试了以下同时使用ivar和动态创建一个新的实例变量,但似乎没有任何效果:

NSString *eventNotes = [NSString stringWithFormat:@"%@", [currentEvent notes]];
eventNotes = [currentEvent notes];
eventNotes = currentEvent.notes;
NSString *eventNotes = [currentEvent notes];
NSString *eventNotes = currentEvent.notes;
NSString *eventNotes = [NSString stringWithString:[currentEvent notes]];
NSString *eventNotes = [NSString stringWithString:currentEvent.notes];
eventNotes = [NSString stringWithString:currentEvent.notes];

名单还在继续,这是我的 cellForRowAtIndexPathCode,发生泄漏的地方。我对内存管理完全陌生,所以请!是描述性的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

     //Custom Cell...
     UTATableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
     cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableviewCell.png"]];
     cell.textLabel.backgroundColor = [UIColor clearColor];
     cell.detailTextLabel.backgroundColor = [UIColor clearColor];

     //reset cell view attributes
     cell.paidButton.tag = 1;
     [cell.paidButton setAlpha:1.0f];
     [cell.paidButton setSelected:NO];
     [cell.paidButton setEnabled:YES];

     cell.title.tag = 2;
     cell.title.text = NULL;

     cell.subtitle.tag = 3;
     cell.subtitle.text = NULL;

     [cell.paidButton addTarget:self action:@selector(togglePaid:) forControlEvents:UIControlEventTouchUpInside];

     if ([_yearSwitcher selectedSegmentIndex] == 0) {

          //lastYearsUTAEvents is an MSMutable array and a property of sharedEventData, which is a singleton class...             
          NSMutableArray *currentArray = [sharedEventData.lastYearsUTAEvents objectAtIndex:indexPath.section];
          EKEvent *currentEvent = [currentArray objectAtIndex:indexPath.row];
          NSDate *date = [currentEvent startDate];
          NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
          [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
          [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
          NSString *dateString = [dateFormatter stringFromDate:date];
          cell.title.text = dateString;

-leak---> eventNotes = [NSString stringWithFormat:@"%@", [currentEvent notes]];

          if ([eventNotes rangeOfString:@" - Paid"].location != NSNotFound) {
               cell.subtitle.text = [eventNotes stringByReplacingOccurrencesOfString:@" - Paid" withString:@""];
               [cell.paidButton setSelected:YES];

          } else {        
               cell.subtitle.text = eventNotes;
               [cell.paidButton setSelected:NO];
          }

      } else {

          // same as above but different array....
      }
  return cell;

}

4

1 回答 1

0

解决方案是在从 EKEvents 数组访问 EKEvent 对象时强制转换 (EKEvent *)

我从日历中获取事件并将它们存储在被NSMutableArray调用lastYearsUTAEvents(这是单例类的属性)中。

在访问数组中的事件时,我不得不使用强制转换来防止由于某种原因导致内存泄漏。所以而不是:

 NSMutableArray *currentArray = [sharedEventData.lastYearsUTAEvents objectAtIndex:indexPath.section];
 EKEvent *currentEvent = [currentArray objectAtIndex:indexPath.row];

我将对象转换为(EKEvent *),同时将其从数组中取出,现在没有任何泄漏。(我还把前面2行代码优化成了1行):

EKEvent *currentEvent = (EKEvent *)[[sharedEventData.thisYearsUTAEvents objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

起初,我什至对能够更改.notes属性感到困惑,因为在文档中,它是一个NSString,而不是一个NSMutableString,所以我假设当你保存一个 EKEvent(或者,在我的情况下,编辑一个)它保存了现有的,因为没有用于保存事件的替换方法。仅保存和删除。

最令人困惑的.notes是,它甚至不是EKEvent. 它是 的财产EKCalendarItem

希望这对将来的人有所帮助...

于 2013-02-02T23:07:35.697 回答