簡體   English   中英

無法使用addObject方法添加到NSMutableArray

[英]Can't add to an NSMutableArray with addObject method

我遇到了NSMutableArrayObject的addObject方法的問題。 這是我現在正在使用的代碼:

- (void)addBirdSightingWithName:(NSString *)name location:(NSString *)location {
    BirdSighting *bird;
    NSDate *today = [NSDate date];
    bird = [[BirdSighting alloc] initWithName:name location:location date:today];
    [self.masterBirdSightingList addObject:bird];
    NSLog(@"Elements: %d", [self.masterBirdSightingList count]);
}

運行此代碼時, NSLog調用將值0打印到控制台。 我不知道是什么原因造成的。

編輯:

我對代碼進行了更深入的研究,發現問題是我的BirdSightingDataController從未初始化。 現在我的問題是:我的BirdSightingDataController的初始化位置在BirdSightingDataController viewDidLoad

感謝大家的幫助。

alloc吃內存masterBirdSightingList

self.masterBirdSightingList = [[NSMutableArray alloc] init];

在幾乎所有不調用cellForRowAtIndexPath:的情況下,都是因為numberOfRowsInSection:返回0。

將日志放在此處,並確保您返回了多個項目,並且您應該能夠看到自己的單元格。 如果您需要進一步的幫助,請將代碼張貼在您的:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

檢查您的屬性訪問器。 您確定返回的對象正確嗎? 確保屬性名稱與實例變量匹配或您已正確指定它(例如: @synthesize masterBirdSightingList = _masterBirdSightingList;如果屬性訪問器與iVar不匹配,則它將返回nil。當然,如果正在手動實現訪問器,請在此處檢查您的代碼。如果不是,則也可以嘗試手動實現該訪問器以確保。

要進行快速檢查,請刪除self.masterBirdSightingList並將其替換為masterBirdSightingList (假設這是iVar名稱)以直接訪問iVar並查看會發生什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM