我正在开发一个用Objective-C编写的iOS应用程序,我有来自Core数据的许多记录的表视图。当我删除一条记录时,该行将从Core数据中删除,但该应用程序正在给我一个错误:
- [_ PFArray removeObjectAtIndex:]:无法识别的选择器发送到实例0x7fbf5be5e6b0
我已多次尝试更改此功能,但它仍然会导致应用程序崩溃,或删除后表视图未更新。
这是我的头文件:
#import <UIKit/UIKit.h>
#include "Notes.h"
@interface ShowClassNote : UIViewController<UITabBarDelegate,UITableViewDataSource>
// get moodle id from segue
@property(strong,nonatomic)NSString*moodeleID;
@property(strong,nonatomic)NSString*moodleName;
@property(strong,nonatomic)NSString*content;
@property (nonatomic, strong) NSMutableArray *fetchedObjects;
@property(strong)NSManagedObjectContext*passThis;
@property(strong,nonatomic)NSString*dataForSend;
@property(strong,nonatomic)NSArray*noteID;
@property(strong,nonatomic)NSArray*noteTitle;
@property(strong,nonatomic)NSArray*insertDates;
@property(strong,nonatomic)NSArray*noteContent;
@property (weak, nonatomic) IBOutlet UILabel *classTitleLable;
@property (weak, nonatomic) IBOutlet UITableView *allNotes;
@end
这是我的实施文件:
#import "ShowClassNote.h"
#import "Subjects.h"
#import "NewClassNote.h"
#import "Notes.h"
#import "AllClassNotesCell.h"
@interface ShowClassNote ()
@end
@implementation ShowClassNote
- (void)viewDidLoad {
[super viewDidLoad];
id delegate =[[UIApplication sharedApplication]delegate];
NSError*error=nil;
NSManagedObjectContext *context = [delegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Notes"
inManagedObjectContext:context];
NSPredicate * predicate =[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"moodleCode=='%@'",self.moodeleID]];
[fetchRequest setPredicate:predicate];
[fetchRequest setEntity:entity];
self.fetchedObjects = [[NSMutableArray alloc]initWithObjects:[context executeFetchRequest:fetchRequest error:&error], nil];
// self.fetchedObjects =(NSMutableArray *) [context executeFetchRequest:fetchRequest error:&error];
// NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
self.noteTitle =[[[NSArray alloc]initWithArray:_fetchedObjects]valueForKey:@"title"];
self.insertDates =[[[NSArray alloc]initWithArray:_fetchedObjects]valueForKey:@"insertDate"];
self.noteContent =[[[NSArray alloc]initWithArray:_fetchedObjects]valueForKey:@"content"];
self.noteID=[[[NSArray alloc]initWithArray:_fetchedObjects]valueForKey:@"moodleCode"];
self.classTitleLable.text=self.moodleName;
self.title=self.moodleName;
[self.allNotes reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// This will create a "invisible" footer
return 0.01f;
}
#pragma mark - Table view data source
- (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 [self.fetchedObjects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * cellIdnt =@"noteCell";
AllClassNotesCell *cell =(AllClassNotesCell *) [tableView dequeueReusableCellWithIdentifier:cellIdnt forIndexPath:indexPath];
// Configure the cell...
// Get current date & time
NSDate *currDate = [self.insertDates objectAtIndex:indexPath.row];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd/MM/YY HH:mm"];
NSString* dateToString =[dateFormatter stringFromDate:currDate];
cell.textLabel.text= [self.noteTitle objectAtIndex:indexPath.row] ;
cell.detailTextLabel.text=dateToString;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 78;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
id delegate =[[UIApplication sharedApplication]delegate];
// Delete the role object that was swiped
NSUInteger currentSelect = indexPath.row;
NSManagedObjectContext *context = [delegate managedObjectContext];
Notes * roleToDelete= [self.fetchedObjects objectAtIndex:currentSelect];
[context deleteObject:roleToDelete];
// Save the context.
NSError *error;
if (![context save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
[_fetchedObjects removeObjectAtIndex:currentSelect];
[self.allNotes reloadData];
}
-(void)viewWillAppear:(BOOL)animated{
id delegate =[[UIApplication sharedApplication]delegate];
NSError*error=nil;
NSManagedObjectContext *context = [delegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Notes"
inManagedObjectContext:context];
NSPredicate * predicate =[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"moodleCode=='%@'",self.moodeleID]];
[fetchRequest setPredicate:predicate];
[fetchRequest setEntity:entity];
self.fetchedObjects = (NSMutableArray *)[context executeFetchRequest:fetchRequest error:&error];
self.noteTitle =[[[NSArray alloc]initWithArray:_fetchedObjects]valueForKey:@"title"];
self.insertDates =[[[NSArray alloc]initWithArray:_fetchedObjects]valueForKey:@"insertDate"];
self.classTitleLable.text=self.moodleName;
self.title=self.moodleName;
[self.allNotes reloadData];
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier]isEqual:@"newNote"]) {
NewClassNote * newNoteView =[segue destinationViewController];
newNoteView.moodleID =self.moodeleID;
newNoteView.newOrOld =YES;
}else if ([[segue identifier]isEqual:@"showNote"])
{
NewClassNote * dvc =[segue destinationViewController];
NSUInteger currentSelect = [self.allNotes indexPathForSelectedRow].row;
NSLog(@"Send is %lu",(unsigned long)currentSelect);
dvc.returnData =[self.fetchedObjects objectAtIndex:currentSelect];
NewClassNote *destViewController = segue.destinationViewController;
destViewController.comingData = [self.noteTitle objectAtIndex:[[self.allNotes indexPathForSelectedRow] row]];
destViewController.insertDate= [self.insertDates objectAtIndex:[[self.allNotes indexPathForSelectedRow] row]];
[self.allNotes reloadData];
}
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
@end
你能解释一下为什么我会收到这个错误吗?删除后的总行数与删除前的行数不同,但我不知道为什么会出现这种错误?
答案 0 :(得分:4)
你的错误来自这里:
self.fetchedObjects = (NSMutableArray *)[context executeFetchRequest:fetchRequest error:&error];
-executeFetchRequest:error:
返回NSArray,它是不可变的,如果你只是将它强制转换为NSMutableArray,它就不会神奇地改变自己。但是后来你试着
[_fetchedObjects removeObjectAtIndex:currentSelect];
将-removeObjectAtIndex
发送到NSArray实例。
您可以尝试解决此问题:
self.fetchedObjects = [[context executeFetchRequest:fetchRequest error:&error] mutableCopy];