在 Xcode Document App 中:为什么 MyDocument 的 init 方法会被调用两次?
我按照 Hillegass 的第 8 章在那里实现了 RaiseMan 应用程序。 然后我决定遵循相同的过程来实现我正在参加的 Cocoa 编程课程中的练习代码,但在构建和运行后我收到了以下非常神秘的错误消息。
无法从对象 <_NSControllerObjectProxy: 0x100460e30> 创建 BOOL类 _NSControllerObjectProxy
我不知道这个错误消息是什么意思。谷歌搜索带来了一些点击,但他们的补救措施似乎是做我已经在做的事情。
我盯着在 Interface Builder 中所做的所有连接和分配,没有发现明显的错误。
因此,我进入调试器并在 MyDocument 类的 init 方法中设置了一个断点,并且它被调用了两次。怎么会发生这种事?我应该寻找什么才能使 init 方法被调用两次?堆栈跟踪显示 init 是由我们自己编写的系统函数调用的。
为了进行比较,我回到了 Hillegass 第 8 章之后的项目,并在 MyDocument 类的 init 方法中设置了一个断点,并且它被调用一次(这是人们所期望的)。
I followed Chapter 8 of Hillegass to implement the RaiseMan application there.
Then I decided to follow the same process to implement the code for an exercise in a Cocoa programming class that I am taking, but I got the following very cryptic error message after building and running.
Cannot create BOOL from object <_NSControllerObjectProxy: 0x100460e30> of class _NSControllerObjectProxy
I have no idea what this error message means. Doing a Google search brought up some hits, but their remedies seemed to be to do things that I was already doing.
I stared at all the connections and assignments that I made in Interface Builder and nothing looks obviously wrong.
So I went into the debugger and set a breakpoint inside the init method of the MyDocument class and it is being called twice. How could that happen? What should I be looking for that would make the init method be called twice? The stack trace shows that init is called by system functions that we did not write ourselves.
For comparison, I went back to the project that follows Chapter 8 of Hillegass and set a breakpoint inside the init method of the MyDocument class, and it is being called once ( which is what one would expect ).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您已将
BOOL
属性绑定到控制器,而不是指定模型关键路径。最有可能的是,您绑定了 Cocoa 视图类的内置绑定之一,例如enabled
或editable
。检查您的笔尖以查找已绑定的
启用
或可编辑
视图,并确保它们全部绑定到正确的模型键路径。It appears you've bound a
BOOL
property to a controller, and not specified a model key path. Most probably, you bound one of the Cocoa view classes' built-in bindings, such asenabled
oreditable
.Look through your nib for views whose
enabled
oreditable
you've bound, and make sure they are all bound to the correct model key path.我自己也遇到过这个。然后我想起以前看到过一些奇怪的东西,当时我并没有意识到它的重要性。在我的 XIB 文件中,除了“文件所有者”对象(它实际上代表 XIB 文件中的文档)之外,还有一个“我的文档”对象。我不知道它是如何到达那里的,但我在 IB 中删除了它,重新编译,然后,[MyDocument init] 现在只被调用一次。
I just ran into this myself. And then I remembered seeing something odd before, whose significance hadn't struck me at the time. Which is that in my XIB file, there was a "My Document" object, in addition to the "File's Owner" object (which is what actually represents the document in the XIB file). I have no idea how it got there, but I deleted it in IB, recompiled, and presto, [MyDocument init] only gets called once now.