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
4

1 回答 1

1

您的问题是您正在将键入为基类的结果分配给特定的子类。正如您所说,这是您收到警告的地方:

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

'NSFetchedResultsController' 返回一个类型为 'id' 的对象

- (id)objectAtIndexPath:(NSIndexPath *)indexPath

(我想它也可以返回一个 NSManagedObject *),但是你将它分配给一个 BookInfo 对象。由于这里不匹配,您需要将返回值转换为您知道的值:

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

你的警告就会消失。

于 2012-09-24T21:42:21.250 回答