2013-07-15 22 views
0

在我的頭文件中,我有兩個屬性,如下所示。實例變量賦值的順序會凍結iOS應用程序

@interface HZCalendarDataSource : NSObject 
@property (strong, nonatomic) NSMutableArray *datesOnCalendar; 
@property (strong, nonatomic) HZCalendarDay *currentDay; 
@end 

然後在我的實現的初始化程序中,我有以下幾行代碼。

- (id)init { 
    self = [super init]; 

    if (self) { 
     // Alloc/Init instance variables. 
     self.datesOnCalendar = [[NSMutableArray alloc] init]; 
       self.currentDay = [[HZCalendarDay alloc] init]; 

     HZCalendarDay *date = [[HZCalendarDay alloc] initOnDate:today withEventStore:self.eventStore]; 

     [self.datesOnCalendar addObject:date]; 
     // THIS line causes the app to freeze! 
     // If this line is above [self.datesOnCalendar addObject:date]; 
     // Then it does not freeze. Why does this happen? 
     self.currentDay = date; 
    } 

    return self; 
} 

,我有問題,是如圖所示的意見,self.currentDay = date;行凍結設備上的應用程序。但是,如果我將self.currentDay = date;行移動到將日期對象添加到NSMutableArray的行的上方,那麼代碼就可以正常工作。

所以我的問題是,爲什麼這件事的順序?它應該只是設置self.currentDay引用相同的date對象,我添加到NSMutableArray正確?

如果有人能夠向我解釋這一點,我會很感激,我不理解它。順序並不重要,所以現在我已經將添加日期對象添加到數組之前執行麻煩的一行,但出於教育目的,我想知道爲什麼這是第一個問題地點。

編輯:

讓冷凍了一段時間的應用程序運行後,它終於未能在Xcode調用[HZCalendarDateSource setCurrentDay:]經過25827次。它在調試器中失敗並且EXC_BAD_ACCESSS和 - [__ NSArrayM countByEnumeratingWithState:objects:count:];

希望這會有所幫助。

謝謝!

回答

0

所以我想通了,在重新閱讀錯誤信息後,聽起來像是因爲某種原因setter失敗了,即使我沒有實現它。

但是,我寫了一個輔助方法來遍歷我的數組,並對與我提供的參數相匹配的項執行一些操作。我曾打電話給幫助方法setCurrentDay:,因此我有一個命名衝突。我的代碼陷入了我寫的for循環中。 for循環掃描self.datesOnCalendar屬性,並且在將對象添加到數組之前執行self.currentDay = date;時,將返回setCurrentDay:方法,因爲該數組爲空。將對象添加到數組後,設置currentDay導致我的setCurrentDay:方法陷入循環。

修復方法是將setCurrentDay:方法重命名爲不與currentDay屬性的setter衝突,同時調整我的for-loop。