【问题标题】:Issue Unknown type name NSManagedObject问题未知类型名称 NSManagedObject
【发布时间】:2012-02-18 05:33:58
【问题描述】:

我正在尝试拥有一个非常简单的核心数据。我有一个这样的 ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIResponder <UIApplicationDelegate>;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (IBAction)boutonSave:(id)sender;

@end

还有一个像这样的 ViewController.m :

#import "ViewController.h"

@implementation ViewController

@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;

- (IBAction)boutonSave:(id)sender {
ViewController *viewController = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [viewController managedObjectContext];
NSManagedObject *newJournee; //THIS LINE IN ERROR
newJournee = [NSEntityDescription insertNewObjectForEntityForName:@"JourneeDeTravail" inManagedObjectContext:context];
[newJournee setValue:_displayStart.text forKey:@"debut"];
[newJournee setValue:_displayEnd.text forKey:@"fin"];    

}

但是在创建 newJournee 时,我有一个红色警报“未知类型名称 'NSManagedObject',你的意思是 NSManagedObjectModel 吗?” ???这是我的第一个核心数据,所以我当然错过了一些对你来说很明显的东西……但对我来说不是!救命!

【问题讨论】:

  • 听起来你错过了#import
  • 是的,你是对的!在开头添加 #import 确实解决了它!谢谢!

标签: core-data ios5 nsmanagedobject


【解决方案1】:

添加CoreData.framework并导入如下

#import <CoreData/CoreData.h>

希望对你有帮助~

【讨论】:

    【解决方案2】:

    听起来你错过了#import

    【讨论】:

    • 是的,在导入时遇到了类似的问题 - Prefix.pch --> // // 前缀头 // // 此文件的内容隐式包含在每个源文件的开头。 // #import #ifndef IPHONE_5_0 #warning "此项目使用仅在 iOS SDK 5.0 及更高版本中可用的功能。" #endif #ifdef __OBJC #import #import #import #endif
    【解决方案3】:

    别担心。一切似乎都很好..

    只需做一件事..转到您的 ViewController.m 文件并添加这一行。

    #import "ViewController.h"
    #import <CoreData/CoreData.h>
    
    @implementation ViewController
    
    @synthesize managedObjectContext = __managedObjectContext;
    @synthesize managedObjectModel = __managedObjectModel;
    @synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
    
    - (IBAction)boutonSave:(id)sender {
    ViewController *viewController = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [viewController managedObjectContext];
    NSManagedObject *newJournee; //Error removed
    newJournee = [NSEntityDescription insertNewObjectForEntityForName:@"JourneeDeTravail" inManagedObjectContext:context];
    [newJournee setValue:_displayStart.text forKey:@"debut"];
    [newJournee setValue:_displayEnd.text forKey:@"fin"];
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      • 2022-01-19
      • 2014-11-19
      • 2018-10-27
      • 2016-09-16
      相关资源
      最近更新 更多