2011-01-29 85 views
0

在我的UITableViewController類,我有這樣的getter函數的NSFetchResultsControllerEXC_BAD_ACCESS:核心數據,NSFetchedResultsController和numberOfRowsInSection

-(NSFetchedResultsController*) fetchedResultsController { 

    if (_fetchedResultsController == nil) { 
     FlipPadAppDelegate* app = (FlipPadAppDelegate*) [[UIApplication sharedApplication] delegate]; 
     NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init]; 
     [fetchRequest setEntity: 
      [NSEntityDescription entityForName:@"FundClass" inManagedObjectContext:[app managedObjectContext]]]; 
     NSSortDescriptor* sortdesp = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 
     [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortdesp]]; 

     [fetchRequest setFetchBatchSize:20]; 

     _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                     managedObjectContext:[app managedObjectContext] 
                     sectionNameKeyPath:nil 
                        cacheName:@"_Stock"]; 


     _fetchedResultsController.delegate = self; 
     [fetchRequest release]; 
     [sortdesp release]; 

     NSError *error; 
     if (![[self fetchedResultsController] performFetch:&error]) { 
      // Handle error 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 

     } 
    } 

    return _fetchedResultsController; 

} 

不知何故,當應用程序運行到這一點,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[[self fetchedResultsController] sections] objectAtIndex:section]; 
    NSLog(@"numberOfRowsInSection %@", [sectionInfo numberOfObjects]); 
    return [sectionInfo numberOfObjects]; 

} 

它發生火災EXC_BAD_ACCESS

sqllite數據庫中有兩條記錄。

爲什麼這個例外?是否因爲我已將sectionNameKeyPath設置爲零?或者因爲我沒有將視圖的dataSource設置爲這個控制器?

+0

崩潰日誌說什麼? – WrightsCS

回答

4

因爲你正在登錄的對象@ 「numberOfRowsInSection%@」 和numberOfObjects是一個int:

@property (nonatomic, readonly) NSUInteger numberOfObjects 

使用@ 「numberOfRowsInSection%我」 代替。

+0

非常感謝!這正是問題所在!我一直在尋找文檔的天,但我從來沒有想過它可能是一個NSLog /格式字符串問題。 –

+0

很高興能幫到你! – v01d

相關問題