2012-08-10 101 views
1

对不起,接下来的新事物。非常感谢您的耐心。NSManagedObjectContext,声明在哪里?

当添加一个新的对象,以核心数据,正确的初始化是这样的:

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

好,所以initWithEntity部分我明白了。我的核心数据模型中只有一个实体,所以我把它放在那里。上下文是我困惑的地方。首先,在哪里声明上下文,还是我需要声明它?只需键入self.ManagedObjectContext不起作用,也不自动完成。也许因为我试图从我的AddViewController中调用此方法是原因,所以即使我输入Car.ManagedObjectContext或AppDelegate.ManagedObjectContext也会发生同样的情况。我猜我可以在我的care-data生成的模型类(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 

回答

4

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

+0

谢谢!我已经浏览了大量的苹果文档,但我一定会掩饰这一点。尽管我只对一件事感到困惑。我可以看到在AppDelegate中声明了managedObjectContext方法,但是我在哪里调用此方法?我试着把它放在AddViewController中,作为NSClass的SaveCar方法的第一行:NSManagedObjectContext * context = [AppDelegate managedObjectContext];但是我得到了编译器的“没有已知的类方法”错误。 – 2012-08-10 10:15:01

+0

您可以像这样获得应用程序委托:(MyAppDelegateClass *)[[UIApplication sharedApplication] delegate]; [更多这里](http://stackoverflow.com/questions/817259/can-you-access-application-delegate-from-within-a-uitableviewdatasource-function) – CasperStromberg 2012-08-10 14:01:33