1

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

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

- (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
4

1 回答 1

4

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

于 2012-08-10T01:15:21.753 回答