NSManagedObjectContext,声明在哪里?

Posted

技术标签:

【中文标题】NSManagedObjectContext,声明在哪里?【英文标题】:NSManagedObjectContext, declared where? 【发布时间】:2012-08-10 01:08:42 【问题描述】:

对于接下来的新手感到抱歉。非常感谢您的耐心。

向核心数据添加新对象时,正确的初始化器是这样的:

- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:    (NSManagedObjectContext *)context

好的,我理解 initWithEntity 部分。我的核心数据模型中只有一个实体,所以我把它放在那里。上下文是我感到困惑的地方。首先,在哪里声明上下文,或者我什至需要声明它?简单地键入 self.ManagedObjectContext 不起作用,也不会自动完成。也许是因为我试图从我的 AddViewController 调用此方法的原因,所以即使我键入 Car.ManagedObjectContext 或 AppDelegate.ManagedObjectContext 也会发生同样的事情。我猜我可以在我的护理数据生成模型类(Car.h)中声明它,但它实际上是做什么的?

我在这里不明白什么?对不起,新手的问题。几个小时以来,我真的一直试图弄清楚这一点。

这是我的代码。

汽车.h:

@interface Car : NSManagedObject

@property (nonatomic, retain) NSString * brand;
@property (nonatomic, retain) NSString * model;
@property (nonatomic, retain) NSString * year;
@property (nonatomic, retain) NSString * color;
@property (nonatomic, retain) NSNumber * engineSize;
@property (nonatomic, retain) NSNumber * weight;
@property (nonatomic, retain) id image;

@end

car.m:

#import "Car.h"


@implementation Car

@dynamic brand;
@dynamic model;
@dynamic year;
@dynamic color;
@dynamic engineSize;
@dynamic weight;
@dynamic image;

@end

addViewController.h(不包括 AppDelegate,因为它几乎都是标准的,而且似乎工作正常。我所做的所有编码都在 addview 控制器中):

#import <Cocoa/Cocoa.h>

@interface AddViewController : NSWindowController
  

@property (weak) IBOutlet NSTextField *brandField;
@property (weak) IBOutlet NSTextField *modelField;
@property (weak) IBOutlet NSTextField *yearField;
@property (weak) IBOutlet NSTextField *weightField;
@property (weak) IBOutlet NSTextField *engineSizeField;
@property (weak) IBOutlet NSTextField *colorField;
@property (weak) IBOutlet NSImageView *imageField;


- (IBAction)saveCar:(id)sender;

@end

AddViewController.m:


#import "AddViewController.h"
#import "AppDelegate.h"
#import "Car.h"
@interface AddViewController ()

@end

@implementation AddViewController
@synthesize brandField;
@synthesize modelField;
@synthesize yearField;
@synthesize engineSizeField;
@synthesize weightField;
@synthesize colorField;
@synthesize imageField;



- (id)initWithWindow:(NSWindow *)window

    self = [super initWithWindow:window];
    if (self) 
        // Initialization code here.
    

    return self;


- (void)windowDidLoad

    [super windowDidLoad];

    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.


- (IBAction)saveCar:(id)sender 
    NSManagedObjectContext *context = [Car managedObjectContext]; //This doesn't work here   "no known class method"


    Car *newCar = [[Car alloc] initWithEntity:@"Car" insertIntoManagedObjectContext:Car.managedObjectContext]; //compiler complains about this, property not found.


    newCar.brand = [brandField stringValue];
    newCar.model =  [modelField stringValue];
    newCar.year =  [yearField stringValue];
    newCar.weight = [weightField objectValue];
    newCar.engineSize = [engineSizeField objectValue];
    newCar.color = [colorField stringValue];
    newCar.image = imageField;





@end

【问题讨论】:

【参考方案1】:

您需要创建一个 managedObjectContext。 它通常在 appDelegate 中完成。 Apples 有一篇关于它的好文章:Apple documentation

【讨论】:

谢谢!我浏览了大量的 Apple 文档,但我一定忽略了这一点。我只对一件事感到困惑。我可以看到在 AppDelegate 中声明 managedObjectContext 方法的位置,但是我从哪里调用此方法?我尝试将它作为 NSManagedObjectContext *context = [AppDelegate managedObjectContext]; 放在 AddViewController 作为我 saveCar 方法的第一行但我从编译器收到“未知的类方法”错误。 您可以像这样获取应用程序委托: (MyAppDelegateClass*)[[UIApplication sharedApplication] delegate]; More here

以上是关于NSManagedObjectContext,声明在哪里?的主要内容,如果未能解决你的问题,请参考以下文章

NSManagedObjectContext 并发

NSManagedObjectContext 的 userInfo 属性是如何工作的?

无法使用CoreData调用非函数类型'NSManagedObjectContext'的值

父/子 NSManagedObjectContext 不起作用

NSManagedObjectContext:自动更新与否?

NSManagedObjectContext:撤消保存操作?