11

我从:http ://www.appcoda.com/introduction-to-core-data/ 了解了核心数据,但是当我自己开发一个示例项目时,两个文件中出现了许多错误。任何帮助将不胜感激,因为我是 iPhone 开发的新手

    //
//  PupilViewController.m
//  Pupils
//
//  Created by Lukasz Mozdzen on 21.04.2013.
//  Copyright (c) 2013 Lukasz Mozdzen. All rights reserved.
//

#import "PupilViewController.h"

@interface PupilViewController ()
@property (strong) NSMutableArray *pupils;

@end

@implementation PupilViewController


- (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];

    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Pupil"];
    self.pupils = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

    [self.tableView reloadData];
}



- (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.pupils.count;
}

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

    // Configure the cell...
    NSManagedObject *pupil = [self.pupils objectAtIndex:indexPath.row];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@", [pupil valueForKey:@"name"], [pupil valueForKey:@"surname"]]];
    [cell.detailTextLabel setText:[pupil valueForKey:@"telephone"]];

    return cell;
}



@end

错误日志:

/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:36:5: Use of undeclared identifier 'NSFetchRequest'

/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:36:21: Use of undeclared identifier 'fetchRequest'

/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:36:38: Use of undeclared identifier 'NSFetchRequest'

/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:37:62: Use of undeclared identifier 'fetchRequest'

/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:62:5: Unknown type name 'NSManagedObject'; did you mean 'NSManagedObjectModel'?

/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:63:67: Receiver type 'NSManagedObjectModel' for instance message is a forward declaration

/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:64:36: Receiver type 'NSManagedObjectModel' for instance message is a forward declaration

同样在其他文件中:

//
//  PupilDetailViewController.m
//  Pupils
//
//  Created by Lukasz Mozdzen on 21.04.2013.
//  Copyright (c) 2013 Lukasz Mozdzen. All rights reserved.
//

#import "PupilDetailViewController.h"

@interface PupilDetailViewController ()

@end

@implementation PupilDetailViewController


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




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

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

    // Create a new managed object
    NSManagedObject *newPupil = [NSEntityDescription insertNewObjectForEntityForName:@"Pupil" inManagedObjectContext:context];
    [newPupil setValue:self.nameTextField.text forKey:@"name"];
    [newPupil setValue:self.surnameTextField.text forKey:@"surname"];
    [newPupil setValue:self.telephoneTextField.text forKey:@"telephone"];

    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];
}



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

错误日志:

/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:38:5: Unknown type name 'NSManagedObject'; did you mean 'NSManagedObjectModel'?

/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:38:34: Use of undeclared identifier 'NSEntityDescription'; did you mean 'kSecAttrDescription'?

/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:38:34: Bad receiver type 'CFTypeRef' (aka 'const void *')

/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:39:6: Receiver type 'NSManagedObjectModel' for instance message is a forward declaration

/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:40:6: Receiver type 'NSManagedObjectModel' for instance message is a forward declaration

/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:41:6: Receiver type 'NSManagedObjectModel' for instance message is a forward declaration

/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:45:11: Receiver type 'NSManagedObjectContext' for instance message is a forward declaration

任何人都可以帮忙吗?

4

4 回答 4

17

在使用之前,您需要在包中添加 coredata 框架。

正如你所说,你是 iPhone 开发的新手,我建议你在实现之前参考coredata 上的苹果文档。

于 2013-04-22T09:49:18.610 回答
6

除了在项目设置中添加 Core Data 框架外,您还必须#import <CoreData/CoreData.h>在源代码中添加。您可以通过将其#import放入项目的 .pch 文件中为整个项目执行一次,该文件通常位于项目树的“支持文件”组中。

于 2014-12-09T07:26:45.223 回答
1

问题是,从那时起已经过了一段时间,从 iOS 10 开始,ManagedObjectContext已移至PersistentContainer属性内部viewContext。这也是为什么您需要稍微更改AppCoda的代码片段以便它调用上下文的原因#

-(NSManagedObjectContext *)managedObjectContext{

    NSManagedObjectContext *context = nil;
    id delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    // call "persistentContainer" not "managedObjectContext"
    if( [delegate performSelector:@selector(persistentContainer)] ){
        // call viewContext from persistentContainer not "managedObjectContext"
        context = [[delegate persistentContainer] viewContext];
    }

    return context;
}
于 2017-01-21T00:09:58.393 回答
0
import CoreData

在“AppDelegate”和“各自的视图控制器”中为我解决了类似的问题。我将 CoreData 添加到现有项目中,因此错过了这个自动导入 CoreData 框架。

用 Swift 3 测试

于 2016-08-04T21:24:37.790 回答