views:

421

answers:

1

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.

+2  A: 
[<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?

The error message tells you what you're doing wrong: You're trying to bind to the representedObject property of your object controller, but it doesn't have one. Binding to properties that don't exist cannot work.

The Note is the content object of the NSObjectController, so that's the controller key you need to bind to: content.

Peter Hosey
OK, so I have a number of Core Data tutorials that use an NSArrayController and arrangedObjects (none using NSObjectController). I'm assuming if I got back and fully understand how the bindings are working in these examples and then apply that knowledge using an NSObjectController and content I should have it working. If that's true I have one more question. The NSArrayController examples call the add: selector (when the make a new row in a table). Is there an equivalent needed for NSObjectController?Thanks
Mark
The key thing to notice is that in IB, the Controller Key and Model Key Path are separate. There's a reason for that: The first key (the Controller Key) accesses some property of the controller, whose value is some part of your model; the rest of the key path (the Model Key Path) drills down into the model to access more basic objects such as strings and images for the view to display. This is one of many ways in which Cocoa expects and enforces MVC separation in your app.
Peter Hosey
You should ask the `add:` thing as a separate question, since it isn't relevant to this question.
Peter Hosey