我应该在我的最新项目中使用 Cocoa 绑定吗?
我正在启动一个项目,我认为该项目将从绑定中受益(我有一个源列表表,几个浏览器视图等),但我认为如果没有它们,它也是相当可行的,也许更容易理解。 根据我有限的经验,我发现绑定很难排除故障,而且非常“神奇”(例如,很难在任何地方插入日志记录来找出哪里出了问题,一切要么有效,要么无效)。
这只是我的经验不足吗(在这种情况下,我可以坐下来花一些时间来研究我对绑定的理解,并期望事情开始变得更清晰/更容易),或者我最好自己编写所有粘合代码我确信我可以理解并解决问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用绑定。
请注意,您必须遵循 MVC 模式才能充分利用绑定。 这比看起来更容易,因为 Cocoa 现在几乎可以为您完成所有事情:
NSView
和子类(当然)、NSCell
和子类、NSWindow
和子类NSController
和子类(尤其是NSArrayController
)如果您不打算使用 Core Data,那么您可以滚动自己的模型对象,但这很容易。 这些对象的大多数方法都是简单的访问器,如果您的目标是 Leopard,则只需
@synthesize
即可。通常,您无法不编写任何代码,但绑定可以让您编写很少的代码。
推荐阅读:
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:
NSView
and subclasses (of course),NSCell
and subclasses,NSWindow
and subclassesNSController
and subclasses (especiallyNSArrayController
)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:
绑定本质上看起来很神奇。 要理解绑定背后的魔力,我认为必须彻底理解 KVC/KVO。 我的意思确实是彻底。
然而,就我而言(Obj-C 新手——9 个月),一旦我获得了 KVC/KVO 绑定,我就感到很兴奋。 它显着减少了我的粘合代码,让我的生活变得更加轻松。 调试绑定成为确保我的键值更改是可观察的情况。 我发现我可以花更多的时间编写我的应用程序应该执行的操作,而不是确保视图反映数据。
我确实同意,尽管绑定一开始是非常令人生畏的。
Bindings can seem magical in nature. To understand the magic behind bindings, I think one must understand KVC/KVO thoroughly. I really do mean thoroughly.
However, in my case (new to Obj-C -- 9 months), once I got KVC/KVO bindings was a thrill. It has significantly reduced my glue code and made my life significantly easier. Debugging bindings became a case of making sure my key-value changes were observable. I find that I am able to spend more time writing what my app is supposed to do rather than making sure the view reflects the data.
I do agree though that bindings is highly intimidating at first.
我的一般方法是从尽可能多地使用绑定开始,然后看看事情进展如何。 但是,如果某个特定的界面元素开始在使用绑定时出现问题,或者付出的努力超出其价值,那么我会毫不犹豫地转而使用更传统的方法(例如数据源、操作)。 我发现这些事情很难提前预测,但我认为从长远来看,支持绑定更好,只要你在它们不提供的情况下不要过于教条地坚持使用它们任何好处。
My general approach is to start out as much as possible using bindings and see how things go. However, if a particular interface element start to become problematic using bindings, or more effort than it's worth, then I don't hesitate to fall back to using more traditional methods (e.g. data sources, actions) when it makes sense. I've found these things can be pretty hard to predict ahead of time, but I think favoring bindings is better in the long run, as long as you don't get too dogmatic about sticking with them in situations when they don't provide any benefit.
使用绑定一段时间后,我发现它根本不神奇,尽管它是足够先进的技术。 调试绑定接口需要与粘合接口不同的技术,但一旦掌握了这些技术,在重用、可维护性和一致性方面的优势在我看来是非常重要的。
After a while of working with Bindings I've found that it's not magic at all, thought it is sufficiently advanced technology. Debugging a bound interface takes different techniques than a glued interface, but once you have those techniques, the advantages in terms of reuse, maintainability and consistency are IMO significant.
看起来我在我的应用程序中几乎同等地使用了绑定、KVO 和数据源方法。 这确实取决于上下文。 例如,在我的一个项目中,除了主窗口的轮廓视图之外,我几乎在所有地方都使用绑定,它足够复杂,我什至不想尝试将其放入 NSTreeController 中。 同时我还使用 KVO 重新加载 UI 对象并跟踪模型对象中的依赖关系。
在学习高级 Cocoa 主题(例如绑定或核心数据)时要记住的重要一点是,您必须了解它们背后的所有技术; 从数据源协议、通知 KVO 等一切内容。 一旦您有足够的与他们合作的经验,了解“魔法”是如何工作的,您将能够轻松地将更高级别的内容集成到您的应用程序中。
在您的特定情况下,您必须决定是否值得在开发应用程序的基础上花费额外的时间来学习绑定。 如果可能的话,使用绑定开发应用程序的简化原型可能会对您有益,这样您就知道在开始实际项目时如何最好地将各个部分组合在一起。
It seems like I use bindings, KVO and data source methods all about equally in my applications. It really depends on the context. For example, in one of my projects I use bindings just about everywhere except the main window's outline view, which is complex enough that I wouldn't want to even try to fit it into an NSTreeController. At the same time I also use KVO to reload UI objects and track dependancies in my model objects.
The important thing to keep in mind when learning advanced Cocoa topics like Bindings or Core Data is that you must understand all the technologies behind them; everything from data source protocols, notifications KVO, and so one. Once you've had enough experience working with them to know how the "magic" works, you'll be able to integrate the higher level stuff into your application with ease.
In your particular case, you'll have to decide if it's worth the extra time to learn bindings on top of developing your application. If possible, it might benefit you to develop a simplified prototype of your application using bindings, so you know how to best fit the pieces together when you start the actual project.
我的意见是,是的,你应该采用绑定; 该技术现在已被广泛理解且稳定,并且对于您不再需要编写的代码量而言,这是值得的。 当我第一次切换到绑定时,我在使观察和观察对象的生命周期匹配以及 UI 破坏方面遇到了相当大的麻烦,因为它观察的是一个有效的对象,但却是不正确的对象。 一旦您多次看到这些问题,了解如何避免它们以及如何在它们出现时发现它们就变得很简单。 是的。 我仍然希望在调试器中出现“这里的事件导致这里的更新”的痕迹,但我仍然很高兴我采取了这一行动。
My opinion is that yes, you should adopt bindings; the technology is well-understood and stable now, and it's worth doing for the amount of code you no longer need to write. When I first switched to bindings, I had quite a bit of trouble with getting the lifetime of observing and observed objects to match up, and with UI breakages because it was observing a valid object, but the incorrect one. Once you've seen those problems a couple of times, knowing how to avoid them and how to spot them if they do appear becomes straightforward. Ish. I still wish for "this event here caused this update here" traces in the debugger, but I'm still glad I made the move.
出于好奇,我最终确实使用了绑定,几天后它们突然开始“有意义”。 所以我绝对建议您继续并花时间学习它们。
我还发现 Brian Webster 的建议非常有帮助,因为我确实最终以老式的方式做了一些事情,要么是因为绑定不能做我想要的事情,要么是因为使用绑定来做我需要做的事情会非常复杂。绑定。
For the curious, I did end up using bindings and after a couple of days they suddenly just started "making sense". So I would definitely recommend just going ahead and taking the time to learn them.
I also found the advice of Brian Webster quite helpful, as I did indeed end up doing a handful of things the old fashioned way either because bindings couldn't do what I wanted or because it would have been prohibitively complicated to do what I needed using bindings.