gpt4 book ai didi

IOS/Objective-C : App crashes with core data - pressing the save button

转载 作者:行者123 更新时间:2023-11-28 21:13:44 26 4
gpt4 key购买 nike

我正在学习本教程 (https://www.appcoda.com/introduction-to-core-data/),了解如何将核心数据提取到表格 View 中、保存和删除新项目。我相信代码是一样的,但应用程序崩溃是由于:

[AppDelegate managedObjectContext]: unrecognized selector sent to instance 0x7b026800'

这是 TableViewControll 的代码:

#import "TableViewController.h"
#import "DetailViewController.h"
#import "AppDelegate.h"


@interface TableViewController ()
@property (strong) NSMutableArray *lisbonSpots;
@end

@implementation TableViewController
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidLoad {
[super viewDidLoad];

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Spot"];
self.lisbonSpots = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

[self.tableView reloadData];

}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


return self.lisbonSpots.count;

}


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

NSManagedObject *ls = [self.lisbonSpots objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [ls valueForKey:@"name"]]];


return cell;
}

和细节 View :

#import "DetailViewController.h"
#import "TableViewController.h"
#import "AppDelegate.h"

@interface DetailViewController ()<UITextFieldDelegate>


@property (weak, nonatomic) IBOutlet UITextField *spot;

@end

@implementation DetailViewController

- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}


- (IBAction)cancel:(UIBarButtonItem *)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)save:(UIBarButtonItem *)sender {
NSManagedObjectContext *context = [self managedObjectContext];

// Create a new managed object
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Spot" inManagedObjectContext:context];
[newDevice setValue:self.spot.text forKey:@"name"];


NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}

[self dismissViewControllerAnimated:YES completion:nil];
}



- (void)viewDidLoad {
[super viewDidLoad];

_spot.delegate=self;

}
@end

为什么会出现此错误?是我做错了什么还是教程过时了?

编辑:

尽管第一个答案是正确的 - 应用程序运行 - 出现了另一个问题:当我点击保存按钮时它崩溃并显示此日志:

'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Spot''

最佳答案

if ([delegate performSelector:@selector(managedObjectContext)]) {

应该是:

if ([delegate respondsToSelector:@selector(managedObjectContext)]) {

关于IOS/Objective-C : App crashes with core data - pressing the save button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42149454/

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