I've been through a bunch of Core Data examples and the Apple documentation. I'm at a wall after working on this all day.
All I want to happen is I type some text into a text field, save the file, open it again and see the text there.
I made a very very simple Core Data document-based app to experiment. Here are the particulars:
1) The data model has one Entity ("Note") with one attribute ("title") which is an NSString.
2) I created a view controller "ManagingViewController" that loads in a view called "NoteView" into a box in MyDocument.xib without a problem. NoteView.nib has just one NSTextField in it.
ManagingViewController.h
#import <Cocoa/Cocoa.h>
#import "Note.h"
@interface ManagingViewController : NSViewController {
NSManagedObjectContext *managedObjectContext;
IBOutlet NSTextField *title;
}
@property (retain) NSManagedObjectContext *managedObjectContext;
@property (retain, readwrite) NSTextField *title;
@end
and ManagingViewController.m
#import "ManagingViewController.h"
#import "Note.h"
@implementation ManagingViewController
@synthesize managedObjectContext;
@synthesize title;
- (id)init
{
if (![super initWithNibName:@"NoteView" bundle:nil]) {
return nil;
}
return self;
}
@end
I have a NSManagedObject called "Note.h"
#import <CoreData/CoreData.h>
#import "ManagingViewController.h"
@interface Note : NSManagedObject
{
}
@property (nonatomic, retain) NSString * title;
@end
and the .m file:
#import "Note.h"
#import "ManagingViewController.h"
@implementation Note
@dynamic title;
@end
In NoteView.nib my:
1) File's Owner is ManagingViewController and the IBOutlets to the Text Field and the view are connected.
2) I dragged over an NSObjectController object into the Interface Builder document window called "Note Object Controller". I set mode to "Entity" and the Entity Name to "Note". "Prepares content" and "Editable" are checked on. (All the examples I've done and been able to find use an NSArrayController here. I don't need an array controller right? I do want to be able to open multiple windows for the same app but I still don't think I need an arraycontroller? All the examples have a NSTableView and a add button. There's no need for an add button here since I don't have an NSTableView).
3) The NSTextView bindings for value I have it bound to "Note Object Controller" with a controller key of representedObject and a Model Key Path of title.
When I run my app I get
[<NSObjectController 0x20004c200> addObserver:<NSTextValueBinder 0x20009eee0>
forKeyPath:@"representedObject.title" options:0x0 context:0x20009f380] was
sent to an object that is not KVC-compliant for the "representedObject" property.
What am I doing wrong? I want to type in the text field, save the file, open it again and see the text there.