2010-12-12 24 views
0

算我試圖存儲在uitable行數爲變量(失敗),我想這:目標C存儲錶行中可變

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 
NSNumber *rowsNumber = [NSNumber numberWithInt: ([[self.fetchedResultsController sections] objectAtIndex:section])]; 
NSLog(@"%i", rowsNumber); 
return [sectionInfo numberOfObjects]; 
} 

這是行不通的,只有一個記錄6位數字?任何人都可以冒險猜測我可以如何存儲這個?

+0

你只是試圖將[sectionInfo numberOfObjects]存儲到rowsNumber? – 2010-12-12 13:34:01

+0

是的,這就是我想要做的 – benhowdle89 2010-12-12 13:40:33

+0

好的,然後Yuji剛回答你的問題。 – 2010-12-12 13:42:34

回答

2

Objective-C不會自動轉換非對象和對象。 (即它不是「自動裝箱」)。

此外,格式說明符%i而不是 unbox規範。所以,如果你有一個NSNumber*num,你要麼做

NSLog(@"%@",num) // show as an object 

NSLog(@"%d",[num intValue]) // show as an int. 

這行不正確,太:

NSNumber *rowsNumber = [NSNumber numberWithInt: [[self.fetchedResultsController sections] objectAtIndex:section] ]; 

首先,[[self.fetchedResultsController sections] objectAtIndex:section]是一個對象,你剛剛進去

id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 

您不能將它傳遞給numberWithInt:。您可以使用sectionInfo.numberOfObjects獲得行數。所以,行應該是

NSNumber *rowsNumber = [NSNumber numberWithUnsignedInteger:sectionInfo.numberOfObjects]; 
+0

優秀!謝謝 – benhowdle89 2010-12-12 13:43:34