我正在努力弄清楚这段代码有什么问题。我想我不太了解引用计数。在iOS版本中我总是依赖ARC。但我应该快速升级到4.0到7.0编写的代码。运行分析会显示以下错误:
Method返回一个带有+0保留计数的Objective-C对象 &安培;引用计数递增。
该对象现在在行:[self changeSelectionTo:[[gregorian dateFromComponents:currentComps] retain]];
对象泄露:此执行路径中未引用已分配的对象,并且[currentPageView selectDate:selected];
处的保留计数为+1
我是IOS的新手,我似乎无法识别此代码中的问题。非常感谢您的帮助。
@property (nonatomic, retain) id<CalendarViewDelegate> delegate;
@property (nonatomic, readonly) NSDate *selected;
- (id)initWithDelegate:(id<CalendarViewDelegate>)d {
if (self = [super initWithFrame:CGRectMake(0, 0, 320, 480)]) {
delegate = [d retain];
self.backgroundColor = [UIColor whiteColor];
gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
[self createUIControls];
NSDateComponents *currentComps =
[gregorian components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
currentDay = currentComps.day;
currentMonth = currentComps.month;
currentYear = currentComps.year;
showingMonth = currentComps.month;
showingYear = currentComps.year;
[self updateLastAndNextMonthAttributes];
[self updateMonthHeader];
currentPageView = [[CalendarPageView alloc] initWithMonth:showingMonth
andYear:showingYear delegate:self];
currentPageView.frame = CGRectMake(0, 0, currentPageView.bounds.size.width, currentPageView.bounds.size.height);
currentPageView.delegate = self;
[pagesView addSubview:currentPageView];
// the memory leak issue seems to be in this area
[self changeSelectionTo:[[gregorian dateFromComponents:currentComps] retain]];
[currentPageView selectDate:selected];
highlighted = nil;