对不起,接下来的新颖性。非常感谢您的耐心配合。

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

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


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

我在这里不明白什么?对不起,新问题。我真的一直在努力弄清楚这个问题。

这是我的代码。

car.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

最佳答案

您需要创建一个managedObjectContext。
通常在appDelegate中完成。
苹果有一篇很好的文章:Apple documentation

关于objective-c - NSManagedObjectContext,声明在哪里?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11894143/

10-11 22:26