gpt4 book ai didi

iphone - 使用未声明的标识符 - 核心数据

转载 作者:行者123 更新时间:2023-11-29 04:24:20 25 4
gpt4 key购买 nike

嗨,我对 xcode 相当陌生,我很难理解为什么会收到此错误“使用未声明的标识符”,我基本上是在尝试将获取的结果返回到 TableView 。

//  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];

你的警告就会消失。

关于iphone - 使用未声明的标识符 - 核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12568244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com