다음과 같은 새내기를 유감스럽게 생각합니다. 양해 해 주셔서 대단히 감사합니다.NSManagedObjectContext는 어디에 선언 되었습니까?
- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext: (NSManagedObjectContext *)context
좋아, 그래서 initWithEntity 부분은 이해 :
핵심 데이터에 새로운 객체를 추가, 올바른 이니셜이 있습니다. 필자는 핵심 데이터 모델에 하나의 엔티티 만 가지고 있으므로 거기에 넣습니다. 문맥은 내가 혼란스러워하는 곳이다. 첫째, 문맥을 어디에서 선언해야합니까? 아니면 선언해야합니까? 단순히 self.ManagedObjectContext를 입력해도 작동하지 않으며 자동 완성되지 않습니다. 아마도 AddViewController에서이 메서드를 호출하려고하기 때문에 그 이유가 있습니다. Car.ManagedObjectContext 또는 AppDelegate.ManagedObjectContext를 입력해도 똑같은 일이 발생합니다. 내 care-data 생성 모델 클래스 (Car.h)에서 선언 할 수 있다고 추측하지만 실제로 수행하는 작업은 무엇입니까?
여기서는 무엇을 이해하지 못하겠습니까? newb 질문에 대해 유감입니다. 나는 정말로 이것을 몇 시간 동안 알아 내려고 노력해 왔습니다.
여기 내 코드가 있습니다.
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에 선언되었는지 알 수 있지만이 메서드는 어디에서 호출해야합니까? NSContainerContext * context = [AppDelegate managedObjectContext] 같이 내 saveCar 메서드의 첫 번째 행으로 AddViewController에 넣으려고했습니다. 하지만 컴파일러에서 "알려진 클래스 메서드가 없습니다"오류가 나타납니다. –
다음과 같이 앱 대리인을 보유 할 수 있습니다. (MyAppDelegateClass *) [[UIApplication sharedApplication] delegate]; [기타 정보] (http://stackoverflow.com/questions/817259/can-you-access-application-delegate-from-within-a-uitableviewdatasource-function) – CasperStromberg