Cocoa 绑定:NSObjectController 的 returnedObject 属性不符合 KVC 标准

发布于 2024-08-16 09:38:31 字数 2235 浏览 7 评论 0原文

我已经阅读了一堆 Core Data 示例和 Apple 文档。我一整天都在做这件事,现在已经到了墙边了。

我想要发生的就是在文本字段中输入一些文本,保存文件,再次打开它并查看其中的文本。

我制作了一个非常非常简单的基于 Core Data 文档的应用程序来进行实验。具体细节如下:

1)数据模型有一个实体(“Note”)和一个属性(“title”),该属性是一个 NSString。

2)我创建了一个视图控制器“ManagingViewController”,它将名为“NoteView”的视图加载到 MyDocument.xib 中的框中,没有出现问题。 NoteView.nib 中只有一个 NSTextField。

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

和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

我有一个名为“Note.h”的NSManagedObject

#import <CoreData/CoreData.h>
#import "ManagingViewController.h"
@interface Note :  NSManagedObject  
{
}
@property (nonatomic, retain) NSString * title;
@end

和.m 文件:

#import "Note.h"
#import "ManagingViewController.h"
@implementation Note 
@dynamic title;
@end

在NoteView.nib 中,我的:

1) 文件的所有者是ManagingViewController,并且文本字段和视图的IBOutlet 已连接。

2)我将一个 NSObjectController 对象拖到名为“Note Object Controller”的 Interface Builder 文档窗口中。我将模式设置为“实体”,将实体名称设置为“注释”。选中“准备内容”和“可编辑”。 (我已经完成并能够找到的所有示例都在这里使用 NSArrayController。我不需要数组控制器,对吧?我确实希望能够为同一个应用程序打开多个窗口,但我仍然不认为我可以需要一个数组控制器吗?所有示例都有一个 NSTableView 和一个添加按钮,因为我没有 NSTableView,所以这里不需要添加按钮。

3)值的 NSTextView 绑定我将其绑定到“Note Object Controller”,控制器键为representedObject,模型键路径为title。

当我运行我的应用程序时,我得到了

[<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.

什么我做错了?我想在文本字段中输入内容,保存文件,再次打开它并查看其中的文本。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不弃不离 2024-08-23 09:38:31
[> addObserver: forKeyPath:@"representedObject.title" options:0x0 context:0x20009f380] 被发送到“representedObject”属性不符合 KVC 的对象。

我做错了什么?

错误消息告诉您做错了什么:您正在尝试绑定到对象控制器的 representedObject 属性,但它没有。绑定到不存在的属性无法工作。

Note 是 NSObjectController 的内容对象,因此这是您需要绑定到的控制器键:content

[<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文