gpt4 book ai didi

ios - entityForName :nil Issue

转载 作者:行者123 更新时间:2023-12-01 17:12:09 28 4
gpt4 key购买 nike

我正在尝试使用 Core Data 让用户可以添加最喜欢的汽车。用户可以选择品牌(在单独的 tableviewcontroller 中)、模型(文本字段)和年份(文本字段)。这些汽车将显示在 tableview 上。
我想我正确地实现了 CoreData,但我得到“由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'+entityForName: nil 不是合法的 NSManagedObjectContext”。我不知道是什么导致我的 managedObjectContent 等于 nil。

这是第一个 View 。这将显示一系列最喜欢的汽车。

#import "addCar.h"
#import "dreamCar.h"

@implementation dreamCar
@synthesize dreamCarArray;

// Retrieve managed object context and later save the data
-(NSManagedObjectContext *)managedObjectContext{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if([delegate performSelector:@selector(managedObjectContext)]){
context = [delegate managedObjectContext];
}
return context;
}

-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

// Get cars from database
NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Cars"];
dreamCarArray = [[moc executeFetchRequest:fetchRequest error:nil]mutableCopy];

[self.tableView reloadData];
}


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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return dreamCarArray.count;
}

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

// Configure the cell
NSManagedObject *car = [dreamCarArray objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@ %@ %@", [car valueForKey:@"year"],[car valueForKey:@"make"],[car valueForKey:@"model"]]];

return cell;
}

@end

这是用户可以添加汽车的下一个 View :
#import "addCar.h"
#import "addDreamCarMake.h"

@implementation addCar
@synthesize makeText,modelText,yearText,car;

// Retrieve managed object context and later save the data
-(NSManagedObjectContext *)managedObjectContext{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if([delegate performSelector:@selector(managedObjectContext)]){
context = [delegate managedObjectContext];
}
return context;
}


-(void)viewDidLoad{
[super viewDidLoad];

self.modelText.delegate = self;
self.yearText.delegate = self;

if(car){
[self.makeText setText:[car valueForKey:@"make"]];
[self.modelText setText:[car valueForKey:@"model"]];
[self.yearText setText:[car valueForKey:@"year"]];
}
}

//If cancel button is pressed, pop current view from stack
- (IBAction)cancelButtonPressed:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}

// If done button is pressed
- (IBAction)doneButtonPressed:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];

// Create new Car
NSManagedObject *newCar = [NSEntityDescription insertNewObjectForEntityForName:@"Cars" inManagedObjectContext:context];
[newCar setValue:makeText forKey:@"make"];
[newCar setValue:modelText forKey:@"model"];
[newCar setValue:yearText forKey:@"year"];

//Pop current view from stack
[self.navigationController popToRootViewControllerAnimated:YES];
}

// Unwind segue from addDreamCarMake
- (IBAction)unwindToListFromAddMake:(UIStoryboardSegue *)segue{
addDreamCarMake *segue1 = segue.sourceViewController;
self.makeText.text = segue1.makeName;
}

//Dismiss Keyboard
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

[self.modelText resignFirstResponder];
[self.yearText resignFirstResponder];
}

// Dismiss Keyboard
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if(textField){
[textField resignFirstResponder];
}
return NO;
}
@end

这是用于选择品牌的表格 View :
#import "addCar.h"  
#import "addDreamCarMake.h"

static NSString* makeLabel;

@implementation addDreamCarMake
@synthesize makeName;

- (void)viewDidLoad
{
[super viewDidLoad];

//Initial Dictionary
makes = @{ @"A" : @[@"Acura",
@"AJS",
@"Alfa Romeo",
@"Allard",
@"Allis Chalmers",
@"AM General",
@"AMC",
@"American",
@"American Motors",
@"Amphicar",
@"Ancarrow",
@"Ariel",
@"Arnolt",
@"Arnolt Bristol",
@"Art Work",
@"Assembled",
@"Aston Martin",
@"ASVE",
@"ASVE",
@"Auburn",
@"Audi",
@"Aurora",
@"Austin",
@"Austin-Healey",
@"Avanti",],
@"B" : @[@"Bay Marine",
@"Beck",
@"Bentley",
@"BMW",
@"Borum",
@"Boss",
@"Bourget",
@"Brainerd Robbins",
@"Bricklin",
@"BSA",
@"Bugatti",
@"Buick",],
@"C" : @[@"C Boat",
@"Cadillac",
@"Callaway",
@"Caterpillar",
@"CAV",
@"Century",
@"Checker",
@"Chevrolet",
@"Chris-Craft",
@"Chris-Craft/Dewhurst",
@"Chrysler",
@"Cobra",
@"Concept",
@"Condor",
@"Cord",
@"Crosley",
@"Cushman",
@"Custom",
@"Custom Built",
@"CZ",],
@"D" : @[@"Daewoo",
@"Daihatsu",
@"Daimler",
@"Dan Kidney",
@"Datsun",
@"Delorean",
@"DeSoto",
@"DeTomaso",
@"Diamond",
@"Dingle",
@"DKW",
@"Dodge",
@"Dodge ",
@"Dry Lakes",
@"Duck Boat",
@"Duesenberg",
@"Dunphy",
@"Dyno",],
@"E" : @[@"Eagle",
@"Eddie Trotta",
@"Edmunds-Kenyon",
@"Edsel",
@"Era",
@"Essex",
@"Evinrude",
@"Excalibur",
@"Eysink",],
@"F" : @[@"Factory Five",
@"Fantasy",
@"Farmall",
@"Ferrari",
@"Fiat",
@"Fleetwood",
@"Ford",
@"Forsa",
@"Framed Art",
@"Franklin",
@"Frazer",],
@"G" : @[@"Gar Wood",
@"Gar Wood/Michaud",
@"Gasoline Pump",
@"Geo",
@"Glen-L",
@"GM",
@"GMC",
@"Goodhue & Hawkins",
@"Graham",
@"Greavette",
@"Griffith",],
@"H" : @[@"Hacker",
@"Hackercraft",
@"Harley-Davidson",
@"Hendrick Motorsports",
@"Henry J",
@"Hickman",
@"Higgins ",
@"Hillman",
@"Hogzoom",
@"Holden",
@"Honda",
@"Horex",
@"Hudson",
@"Hummer",
@"Hunter",
@"Hupmobile",
@"Hutchinson",
@"Hyundai",],
@"I" : @[@"IH",
@"IH Farmall",
@"Imperial",
@"Indian",
@"Indian Lakes",
@"Infiniti",
@"International",
@"Isotta",
@"Isuzu",],
@"J" : @[@"Jaguar",
@"JBL",
@"Jeep",
@"Jensen",
@"John Deere",
@"Johnson",],
@"K" : @[@"Kaiser",
@"Kia",
@"KR Proton",
@"Kurtis",],
@"L" : @[@"Lagonda",
@"Lamborghini",
@"Land Rover",
@"Larson",
@"Lasalle",
@"Lexus",
@"Lighted Sign",
@"Lincoln",
@"Lola",
@"Lotus",
@"Lucky Fire",
@"Luyere",
@"Lyman",],
@"M" : @[@"MAC Tools",
@"Mack",
@"Malibu",
@"Maserati",
@"Massey-Harris",
@"Matchless",
@"Maxton",
@"Mazda",
@"McCormick",
@"McLaren",
@"Memorabilia",
@"Mercedes-Benz",
@"Mercury",
@"Merkur",
@"Metal Sign",
@"MG",
@"Midget",
@"Mini",
@"Mitsubishi",
@"Model",
@"Moore",
@"Morris",
@"Mugen",
@"Murray",
@"MV Agusta",],
@"N" : @[@"N.U.T.",
@"Nash",
@"Neon Sign",
@"Nissan",
@"Norton",
@"NSU",],
@"O" : @[@"Oakland",
@"OCC",
@"Oldsmobile",
@"Oliver Hart-Parr",],
@"P" : @[@"Pabst ",
@"Pace Arrow",
@"Packard",
@"Panoz",
@"Panther",
@"Parts",
@"Penn Yan",
@"Peugeot",
@"Pierce-Arrow",
@"Plastic Sign",
@"Plymouth",
@"Pontiac",
@"Porcelain Sign",
@"Porsche",
@"Prevost",
@"Pulse",],
@"Q" : @[@"Qvale",],
@"R" : @[@"Racing ",
@"Rambler",
@"Reading-Standard",
@"Renault",
@"Reo",
@"Replica",
@"Replicar",
@"Retrovette",
@"Revcon",
@"Richardson",
@"Riva",
@"Rolls-Royce",],
@"S" : @[@"Saab",
@"Saleen",
@"Saturn",
@"Schwinn",
@"Scorpion",
@"Sea Lyon",
@"Seagrave",
@"Shay",
@"Shelby",
@"Shepherd",
@"SIMCA",
@"Sims",
@"Skiff Craft",
@"Speed Sport",
@"Star",
@"Staudacher",
@"Sterling",
@"Steve McQueen",
@"Steyr-Daimler Puch",
@"Stone Boatyard",
@"Studebaker",
@"Subaru",
@"Sudlow",
@"Sunbeam",
@"Sunflower",
@"Superformance",
@"Suzuki",],
@"T" : @[@"Thompson",
@"Tidal Force",
@"Tiffany",
@"Tin Sign",
@"Toyota",
@"Trigre",
@"Triumph",
@"Truscott",
@"TVR",],
@"V" : @[@"Ventnor",
@"Vincent",
@"Volkswagen",
@"Volvo",],
@"W" : @[@"Wagner",
@"Westcott",
@"Whiticar",
@"Whizzer",
@"Willys",
@"Wise & Sons",
@"Wise Boat & Bus Works",],
@"Y" : @[@"Yamaha",
@"Yamaha Castrol",
@"Yellow Jacket",],
@"Z" : @[@"Zimmer",
@"Zundapp",]},

makeSectionTitles = [[makes allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [makeSectionTitles count];
}

// Title at each section
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [makeSectionTitles objectAtIndex:section];
}

// Number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSString *sectionTitle = [makeSectionTitles objectAtIndex:section];
NSArray *sectionModels = [makes objectForKey:sectionTitle];
return [sectionModels count];
}

// Item at each cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addDreamCarMakeCell" forIndexPath:indexPath];

// Configure the cell...
NSString *sectionTitle = [makeSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionmodels = [makes objectForKey:sectionTitle];
NSString *model = [sectionmodels objectAtIndex:indexPath.row];
cell.textLabel.text = model;

return cell;
}

// Index on the left side
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return makeSectionTitles;
}

// If back button is pressed, pop current view from stack
- (IBAction)backButtonPressed:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}


// Segue
-(void) prepareForSegue: (UIStoryboardSegue *)segue sender: (UITableViewCell *)sender {

//Segue back to searchCar
if ([segue.identifier isEqualToString:@"addMakeCompleted"]) {
makeLabel = sender.textLabel.text;
self.makeName = makeLabel;
}
}
@end

这是appdelegate.h
#import <UIKit/UIKit.h>

@interface infoMobileAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (weak, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (weak, nonatomic) NSManagedObjectContext *managedObjectModel;
@property (weak, nonatomic) NSManagedObjectContext *persistentStoreCoordinator;

@end

这是 appdelegate.m
#import "infoMobileAppDelegate.h"

@implementation infoMobileAppDelegate

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}

@end

编辑:
所以在我的 addCar.h 中,我添加了一个名为 car 的 NSManagedObject。 (强,非原子)
在我的 addCar.m 中,当我通过 viewDidLoad 进行调试时,我看到它跳过了 if(car) 语句。我认为这就是导致它为零的原因。
-(void)viewDidLoad{
[super viewDidLoad];

self.modelText.delegate = self;
self.yearText.delegate = self;

if(car){
[self.makeText setText:[car valueForKey:@"make"]];
[self.modelText setText:[car valueForKey:@"model"]];
[self.yearText setText:[car valueForKey:@"year"]];
}
}

再次编辑:
修复。我的应用程序委托(delegate)缺少一些方法。以下是我必须添加的方法,以防有人想知道:
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}

#pragma mark - Core Data stack

// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}

// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"coreDataInfoNet" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}

// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"coreDataInfoNet.sqlite"];

NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.

abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.


If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

return _persistentStoreCoordinator;
}

#pragma mark - Application's Documents directory

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

@end

最佳答案

丢弃 if 怎么样? View Controller 中的语句?

如果您使用“空应用程序”模板启动一个新的 Xcode 项目并单击“使用核心数据”复选框,您将在应用程序委托(delegate)中看到所需的代码

  • 配置 PersistentStoreCoordinator
  • 加载 ManagedObjectModel
  • 打开 ManagedObjectContext

  • 默认项目模板将这些作为属性公开在 AppDelegate 上,因此您可以像这样在应用程序的其他地方引用那些
    // note that you need to import the header to see the properties directly
    #import "AppDelegate.h"

    AppDelegate *myApp = [UIApplication sharedApplication];
    NSManagedObjectContext *moc = myApp.managedObjectContext;
    // use the moc as required....

    这样做的好处是你只有一个方法来设置 ManagedObjectContext,所以如果它的行为不像预期的那样,只有一个地方可以查看......没有将 moc 设置为 nil然后取决于 if语句将其设置为非零值。

    希望有帮助。

    关于ios - entityForName :nil Issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23178433/

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