2012-09-24 40 views
0

嗨,我是相當陌生的xcode,我無法得到我的頭爲什麼即時通訊該錯誤「使用未聲明的標識符」我基本上試圖返回提取結果到表視圖。使用未聲明的標識符 - 核心數據

// UNBSearchBooksViewController.m 

#import "UNBSearchBooksViewController.h" 
#import "NBAppDelegate.h" 

@interface UNBSearchBooksViewController() 

@end 

@implementation UNBSearchBooksViewController 

@synthesize booksSearchBar; 
@synthesize searchTableView; 
@synthesize fetchedResultsController, managedObjectContext; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // NSFetchRequest needed by the fetchedResultsController 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 

    // NSSortDescriptor tells defines how to sort the fetched results 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 

    // fetchRequest needs to know what entity to fetch 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"BookInfo" inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [fetchedObjects count]; 
} 

// Customize the appearance of table view cells. 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell" ; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell "this is where my problem is" use of undeclared identifier 
    BookInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    cell.textLabel.text = info.title; 

    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

} 

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar 
{ 

    if (self.booksSearchBar.text !=nil) 
    { 
     NSPredicate *predicate =[NSPredicate predicateWithFormat:@"bookName contains[cd] %@", self.booksSearchBar.text]; 
     [fetchedResultsController.fetchRequest setPredicate:predicate]; 
    } 
    else 
    { 
     NSPredicate *predicate =[NSPredicate predicateWithFormat:@"All"]; 
     [fetchedResultsController.fetchRequest setPredicate:predicate]; 
    } 

    NSError *error = nil; 
    if (![[self fetchedResultsController] performFetch:&error]) 
    { 
     // Handle error 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     exit(-1); // Fail 
    } 

    fetchedObjects = fetchedResultsController.fetchedObjects; 

    [booksSearchBar resignFirstResponder]; 

    [searchTableView reloadData]; 
} 

- (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{ 
    searchBar.showsCancelButton = YES; 
} 

- (void) searchBarTextDidEndEditing:(UISearchBar *)searchBar 

{ 
    searchBar.showsCancelButton = NO; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{ 
    [searchBar resignFirstResponder]; 
} 
- (void)didReceiveMemoryWarning 
{ 

    [super didReceiveMemoryWarning]; 


} 
- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 

} 
@end 
+3

你爲什麼不開始告訴我們錯誤正在發生什麼行? –

回答

1

你的問題是,你正在分配一個特定的子類作爲基類鍵入的結果。正如你所說,這是你得到警告:

BookInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

「NSFetchedResultsController」返回從

- (id)objectAtIndexPath:(NSIndexPath *)indexPath 

型「身份證」的對象(我想它可能會返回一個NSManagedObject *太) ,但將其分配給BookInfo對象。因爲這裏有一個不匹配,需要返回值轉換爲你知道它是什麼:

BookInfo *info = (BookInfo *)[self.fetchedResultsController objectAtIndexPath:indexPath]; 

和你的警告將會消失。

+0

謝謝你的答覆,通過從xcdatamodeld創建.h和.m文件來聲明BooksInfo實體,解決了問題。 –

+0

我沒有真正回答你的問題,但感謝你選擇它。將來如果您複製並粘貼實際的錯誤,它可以幫助社區更好地幫助您。 –