使用未声明的标识符

使用未声明的标识符

嗨,我对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

最佳答案

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

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


“ NSFetchedResultsController”从中返回“ id”类型的对象

- (id)objectAtIndexPath:(NSIndexPath *)indexPath


(我想它也可能返回NSManagedObject *),但是您将其分配给BookInfo对象。由于此处不匹配,因此您需要将返回值转换为您知道的值:

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


您的警告就会消失。

10-07 15:42