使用核心数据时出现不一致错误
Posted
技术标签:
【中文标题】使用核心数据时出现不一致错误【英文标题】:Getting Inconsistantcy Error When Using Core Data 【发布时间】:2011-04-07 02:54:40 【问题描述】:当我点击下面发布的 viewController 时,我收到一条错误消息:
* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Routine isEqualToString:]:无法识别的选择器发送到实例 0x9c05320”
谁能帮我完成这项工作?
这是我的代码:
@implementation RoutineTableViewController
@synthesize tableView;
@synthesize eventsArray;
@synthesize entered;
@synthesize managedObjectContext;
#pragma mark - View lifecycle
- (void)viewDidLoad
if (managedObjectContext == nil)
managedObjectContext = [(CurlAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Routine" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil)
// Handle the error.
[self setEventsArray:mutableFetchResults];
[mutableFetchResults release];
[request release];
UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showPrompt)];
[self.navigationItem setLeftBarButtonItem:addButton];
[addButton release];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEdit)];
self.navigationItem.rightBarButtonItem = editButton;
[editButton release];
[super viewDidLoad];
- (void)viewDidUnload
self.eventsArray = nil;
[super viewDidUnload];
-(void)toggleEdit
[self.tableView setEditing: !self.tableView.editing animated:YES];
if (self.tableView.editing)
[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
else
[self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
- (void)dealloc
[managedObjectContext release];
[eventsArray release];
[super dealloc];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
#pragma mark -
#pragma mark Add an event
-(void)addEvent
Routine *routine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];
routine.name=entered;
NSError *error = nil;
if (![managedObjectContext save:&error])
// Handle the error.
NSLog(@"%@", error);
[eventsArray insertObject:routine atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
-(void)showPrompt
AlertPrompt *prompt = [AlertPrompt alloc];
prompt = [prompt initWithTitle:@"Add Workout Day" message:@"\n \n Please enter title for workout day" delegate:self cancelButtonTitle:@"Cancel" okButtonTitle:@"Add"];
[prompt show];
[prompt release];
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
if (buttonIndex != [alertView cancelButtonIndex])
entered = [(AlertPrompt *)alertView enteredText];
if(eventsArray && entered)
[eventsArray addObject:entered];
[tableView reloadData];
[self addEvent];
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [eventsArray count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
// Dequeue or create a new cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
Routine *routine = (Routine *)[eventsArray objectAtIndex:indexPath.row];
cell.textLabel.text = [self.eventsArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
// Return NO if you do not want the specified item to be editable.
return YES;
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
if (editingStyle == UITableViewCellEditingStyleDelete)
// Delete the managed object at the given index path.
NSManagedObject *eventToDelete = [eventsArray objectAtIndex:indexPath.row];
[managedObjectContext deleteObject:eventToDelete];
// Update the array and table view.
[eventsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
// Commit the change.
NSError *error = nil;
if (![managedObjectContext save:&error])
// Handle the error.
【问题讨论】:
【参考方案1】:您可能正在使用 Routine
对象,而您应该使用 NSString。如果没有更多关于异常在巨大代码转储中的确切位置的信息,我只能猜测,但这在tableView:cellForRowAtIndexPath:
中看起来特别可疑:
Routine *routine = (Routine *)[eventsArray objectAtIndex:indexPath.row];
cell.textLabel.text = [self.eventsArray objectAtIndex:indexPath.row];
如果数组中的值确实是Routine
,那么将该对象分配给标签的text
属性很可能是错误的。
【讨论】:
是的,这就是错误的来源。我怎样才能解决这个问题?它应该是来自名为 Routine 的实体的 name 属性中的文本。 然后将[routine name]
分配给text
属性。但请注意,据我所知,您已将 Routine
对象和 NSStrings 都插入到 eventsArray 中。您可能应该重组您的代码以不这样做,或者如果绝对必要,您可以使用 isKindOfClass:
来了解任何特定对象是例程(意味着您需要调用 [routine name]
来获取字符串)还是 NSString。
谢谢阿诺米。好的,所以我没有理由在 eventsArray 中有 Routine
对象和 NSStrings。我相信 eventsArray 应该只包含将在表格视图中显示的获取数据,对吗?我怎样才能重组我的代码来做到这一点?
@Faisal:你为什么这么认为?
你说我在 eventsArray 中插入了 Routine 对象和 NSStrings。我认为它应该只适用于 nsstrings 对吧?我不确定如何准确解决这个问题。这是我第一次将 Core Data 添加到应用程序中以上是关于使用核心数据时出现不一致错误的主要内容,如果未能解决你的问题,请参考以下文章
使用 sinon 时出现不一致的 UnhandledPromiseRejectionWarning
PySpark - 运行 Count() / 聚合函数(平均等)时出现不一致
WinHttp WinHttpSendRequest 上传文件 dwTotalLength 参数值 在 C# PHP 中表现不一致