我应该使用Cocoa绑定我的最新项目吗? [英] Should I use Cocoa bindings for my latest project?
问题描述
我开始一个项目,我认为会受益于绑定(我有一个源列表表,几个浏览器视图等),但我认为它也将是可行的,也许更容易理解,没有他们。从我有限的经验,我发现绑定是很难排除故障,非常魔法(例如,很难在任何地方插入日志,找出哪里的东西破碎,一切都行或不行)。
这只是我不经验的谈话(在这种情况下,我可以坐下来花一些时间,只是我的理解绑定,期望事情开始变得更清楚/更容易)或者我会更好
使用绑定。
/ p>请注意,您必须按照MVC模式才能从绑定中获得最大收益。 这似乎比看起来容易,因为Cocoa几乎为你现在做了一切:
- 查看:
NSView
和子类(当然),NSCell
和子类,NSWindow
- 控制器:
NSController
和子类(特别是NSArrayController
) - 模型:Core Data
如果您不打算使用Core Data,滚动自己的模型对象,但这很容易。大多数这些对象的方法将是简单的访问器,如果你的目标是Leopard,你可以 @synthesize
。
推荐阅读:
$ b,你不能写任何代码,$ b
- 键值
- 键值观察(KVO)编程指南
- 模型对象实施指南
- KVC存取器方法(上述KVC编程指南的一部分)和我的完整的KVC兼容存取器选择器格式列表
I'm starting a project which I think would benefit from bindings (I've got a source list table, several browser views, etc), but I think it would also be quite doable, and perhaps more understandable, without them. From my limited experience I've found bindings to be difficult to troubleshoot and very "magic" (e.g. it's difficult to insert logging anywhere to figure out where stuff is breaking, everything either works or it doesn't).
Is this just my inexperience talking (in which case I could sit down and spend some time just working on my understanding of bindings and expect things to start becoming clearer/easier) or would I be better off just writing all the glue code myself in a manner which I was sure I could understand and troubleshoot.
Use Bindings.
Note that you must follow the MVC pattern to get the most from bindings. This is easier than it seems, as Cocoa does almost everything for you nowadays:
- View:
NSView
and subclasses (of course),NSCell
and subclasses,NSWindow
and subclasses - Controller:
NSController
and subclasses (especiallyNSArrayController
) - Model: Core Data
If you're not going to use Core Data, then you get to roll your own model objects, but this is easy. Most of these objects' methods will be simple accessors, which you can just @synthesize
if you're targeting Leopard.
You usually can't get away with not writing any code, but Bindings can enable you to write very little code.
Recommended reading:
- Key-Value Coding (KVC) Programming Guide
- Key-Value Observing (KVO) Programming Guide
- Model Object Implementation Guide
- KVC Accessor Methods (part of the aforementioned KVC Programming Guide) and my complete list of KVC-compliant accessor selector formats
这篇关于我应该使用Cocoa绑定我的最新项目吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!