获取结果崩溃(使用persistentContainer) - 核心数据目标C.

时间:2022-11-07 16:31:09

I'm trying to fetch results from core data to display in table view whenever the view is loaded. Request does fetch the result but crashes as soon as view loads.

我正在尝试从核心数据中获取结果,以便在加载视图时在表视图中显示。请求会获取结果,但一旦视图加载就会崩溃。

with reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance

Since the introduction of Persistent Container, I couldn't find any reference about how to use it using Objective C.

自从引入Persistent Container以来,我找不到有关如何使用Objective C的任何参考资料。

I've a simple Core Data Model, Entity - 'Item' with Attribute - 'name'

我有一个简单的核心数据模型,实体 - 带有属性的'项目' - '名称'

// ViewController.m //

// ViewController.m //

@interface ViewController ()
{
NSMutableArray *listArray;
AppDelegate *delegate;
NSManagedObjectContext *context;
NSMutableArray *resultListArray;
}
@end

- (void)viewDidLoad {
[super viewDidLoad];
listArray = [[NSMutableArray alloc]init];
resultListArray = [[NSMutableArray alloc]init];
[self fetchItems];
}

TableView Data Source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [listArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

if (resultListArray) {
    cell.textLabel.text = [resultListArray objectAtIndex:indexPath.row];
}

cell.textLabel.text = [listArray objectAtIndex:indexPath.row];
return cell;
}

Get Managed Context

- (NSManagedObjectContext *)managedObjectContext {

delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
context = [[delegate persistentContainer]viewContext];

NSLog(@"ManagedContext Created Successfully");

return context;
}

Save to Core Data

- (void) saveItemMethod:(NSString*)name {

context = [self managedObjectContext];

NSManagedObject *task = [[Item alloc]initWithContext:context];

[task setValue:name forKey:@"name"];

NSString *itemString = [task valueForKey:@"name"];

[listArray addObject:itemString];

[delegate saveContext];

NSLog(@"Save successful");
NSLog(@"%@", listArray);
}

Fetch results

- (void) fetchItems {

context = [self managedObjectContext];

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Item"];
// request.resultType = NSDictionaryResultType;

NSError *error = nil;
NSManagedObject *result = (NSManagedObject*)[context executeFetchRequest:request error:&error];

NSString *resultString = [result valueForKey:@"name"];

[resultListArray addObject:resultString];

NSLog(@"Fetch successful");
NSLog(@"%@", resultListArray);

[self.tableView reloadData];
}

1 个解决方案

#1


1  

The error message indicates that the isEqualToString: method is being called on an NSArray object - which obviously doesn't work (isEqualToString: is an NSString method).

错误消息表明正在NSArray对象上调用isEqualToString:方法 - 这显然不起作用(isEqualToString:是一个NSString方法)。

So, your code is treating an array as though it is a string. The root of the problem lies here, in the fetchItems code:

因此,您的代码将数组视为字符串。问题的根源在于fetchItems代码:

NSManagedObject *result = (NSManagedObject*)[context executeFetchRequest:request error:&error];
NSString *resultString = [result valueForKey:@"name"];
[resultListArray addObject:resultString];

The first line is wrong: executeFetchRequest returns an array of NSManagedObjects (even if there is only one object in the array). You can therefore just use:

第一行是错误的:executeFetchRequest返回一个NSManagedObjects数组(即使数组中只有一个对象)。因此你可以使用:

NSArray *result = [context executeFetchRequest:request error:&error];
self.resultListArray = [[result valueForKey:@"name"] mutableCopy];

#1


1  

The error message indicates that the isEqualToString: method is being called on an NSArray object - which obviously doesn't work (isEqualToString: is an NSString method).

错误消息表明正在NSArray对象上调用isEqualToString:方法 - 这显然不起作用(isEqualToString:是一个NSString方法)。

So, your code is treating an array as though it is a string. The root of the problem lies here, in the fetchItems code:

因此,您的代码将数组视为字符串。问题的根源在于fetchItems代码:

NSManagedObject *result = (NSManagedObject*)[context executeFetchRequest:request error:&error];
NSString *resultString = [result valueForKey:@"name"];
[resultListArray addObject:resultString];

The first line is wrong: executeFetchRequest returns an array of NSManagedObjects (even if there is only one object in the array). You can therefore just use:

第一行是错误的:executeFetchRequest返回一个NSManagedObjects数组(即使数组中只有一个对象)。因此你可以使用:

NSArray *result = [context executeFetchRequest:request error:&error];
self.resultListArray = [[result valueForKey:@"name"] mutableCopy];