gpt4 book ai didi

objective-c - NSManagedObjectContext,在哪里声明?

转载 作者:行者123 更新时间:2023-12-03 16:55:00 25 4
gpt4 key购买 nike

对于接下来的新鲜事,我们深表歉意。非常感谢您的耐心等待。

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

- (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 Controller 中):

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com