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.
这只是我缺乏经验的谈话(在这种情况下,我可以坐下来花一些时间来研究我对绑定的理解并期望事情变得更清晰/更容易)或者我会更好地自己编写所有胶水代码我确信我能理解并排除故障的方式。
7 个解决方案
#1
21
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:
请注意,您必须遵循MVC模式才能从绑定中获得最大收益。这比看起来容易,因为Cocoa现在几乎为你做了一切:
- View:
NSView
and subclasses (of course),NSCell
and subclasses,NSWindow
and subclasses - Controller:
NSController
and subclasses (especiallyNSArrayController
) - Model: Core Data
查看:NSView和子类(当然),NSCell和子类,NSWindow和子类
控制器:NSController和子类(尤其是NSArrayController)
型号:核心数据
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.
如果您不打算使用Core Data,那么您可以滚动自己的模型对象,但这很容易。这些对象中的大多数方法都是简单的访问器,如果你的目标是Leopard,你可以@synthesize。
You usually can't get away with not writing any code, but Bindings can enable you to write very little code.
您通常无法编写任何代码,但Bindings可以让您编写非常少的代码。
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
键值编码(KVC)编程指南
键值观察(KVO)编程指南
模型对象实施指南
KVC访问器方法(上述KVC编程指南的一部分)和我的KVC兼容访问器选择器格式的完整列表
#2
8
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.
绑定本质上看起来很神奇。要理解绑定背后的魔力,我认为必须彻底了解KVC / KVO。我真的很清楚。
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.
然而,在我的情况下(Obj-C的新手 - 9个月),一旦我得到KVC / KVO绑定是一种刺激。它大大减少了我的胶水代码,使我的生活变得更加轻松。调试绑定成为确保我的键值更改是可观察的情况。我发现我可以花更多的时间来编写我的应用程序应该做的事情而不是确保视图反映数据。
I do agree though that bindings is highly intimidating at first.
我同意尽管绑定最初是非常令人生畏的。
#3
5
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.
我的一般方法是尽可能使用绑定开始,看看情况如何。但是,如果一个特定的界面元素开始变得有问题,使用绑定,或者比它的价值更多的努力,那么我会毫不犹豫地回到使用更传统的方法(例如数据源,动作)时才有意义。我发现这些事情很难提前预测,但我认为从长远来看,赞成绑定会更好,只要你在没有提供的情况下不要太过教条。任何好处。
#4
4
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.
经过一段时间的使用Bindings后,我发现它根本不是魔术,认为它是足够先进的技术。调试绑定接口采用与粘合接口不同的技术,但是一旦掌握了这些技术,重用,可维护性和一致性方面的优势就显着了。
#5
2
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.
似乎我在我的应用程序中使用绑定,KVO和数据源方法。这实际上取决于具体情况。例如,在我的一个项目中,除了主窗口的大纲视图之外,我几乎使用绑定,这很复杂,我甚至不想尝试将它装入NSTreeController。同时我还使用KVO重新加载UI对象并跟踪模型对象中的依赖项。
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.
在学习Bindings或Core Data等高级Cocoa主题时要记住的重要一点是,你必须了解它们背后的所有技术;从数据源协议,通知KVO等一切。一旦你有足够的经验与他们一起工作以了解“神奇”是如何工作的,你就能够轻松地将更高级别的东西集成到你的应用程序中。
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.
在您的特定情况下,您必须决定在开发应用程序的基础上学习绑定是否值得花费额外的时间。如果可能,使用绑定开发应用程序的简化原型可能会对您有所帮助,因此您知道在启动实际项目时如何最好地将各个部分组合在一起。
#6
2
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.
我的意见是,你应该采用绑定;现在,这项技术已被充分理解和稳定,并且值得为您不再需要编写的代码量做。当我第一次切换到绑定时,我在观察和观察对象的生命周期以及UI断裂时遇到了相当多的麻烦,因为它正在观察一个有效的对象,但是不正确的对象。一旦你看到这些问题几次,知道如何避免它们以及如果它们确实出现如何发现它们变得简单明了。伊什。我仍然希望“此事件在这里引起了此更新”在调试器中的痕迹,但我仍然很高兴我采取了行动。
#7
1
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.
我也发现布莱恩韦伯斯特的建议非常有用,因为我确实最终做了一些老式的方法,因为绑定不能做我想要的,或者因为做我需要的东西会非常复杂绑定。
#1
21
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:
请注意,您必须遵循MVC模式才能从绑定中获得最大收益。这比看起来容易,因为Cocoa现在几乎为你做了一切:
- View:
NSView
and subclasses (of course),NSCell
and subclasses,NSWindow
and subclasses - Controller:
NSController
and subclasses (especiallyNSArrayController
) - Model: Core Data
查看:NSView和子类(当然),NSCell和子类,NSWindow和子类
控制器:NSController和子类(尤其是NSArrayController)
型号:核心数据
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.
如果您不打算使用Core Data,那么您可以滚动自己的模型对象,但这很容易。这些对象中的大多数方法都是简单的访问器,如果你的目标是Leopard,你可以@synthesize。
You usually can't get away with not writing any code, but Bindings can enable you to write very little code.
您通常无法编写任何代码,但Bindings可以让您编写非常少的代码。
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
键值编码(KVC)编程指南
键值观察(KVO)编程指南
模型对象实施指南
KVC访问器方法(上述KVC编程指南的一部分)和我的KVC兼容访问器选择器格式的完整列表
#2
8
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.
绑定本质上看起来很神奇。要理解绑定背后的魔力,我认为必须彻底了解KVC / KVO。我真的很清楚。
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.
然而,在我的情况下(Obj-C的新手 - 9个月),一旦我得到KVC / KVO绑定是一种刺激。它大大减少了我的胶水代码,使我的生活变得更加轻松。调试绑定成为确保我的键值更改是可观察的情况。我发现我可以花更多的时间来编写我的应用程序应该做的事情而不是确保视图反映数据。
I do agree though that bindings is highly intimidating at first.
我同意尽管绑定最初是非常令人生畏的。
#3
5
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.
我的一般方法是尽可能使用绑定开始,看看情况如何。但是,如果一个特定的界面元素开始变得有问题,使用绑定,或者比它的价值更多的努力,那么我会毫不犹豫地回到使用更传统的方法(例如数据源,动作)时才有意义。我发现这些事情很难提前预测,但我认为从长远来看,赞成绑定会更好,只要你在没有提供的情况下不要太过教条。任何好处。
#4
4
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.
经过一段时间的使用Bindings后,我发现它根本不是魔术,认为它是足够先进的技术。调试绑定接口采用与粘合接口不同的技术,但是一旦掌握了这些技术,重用,可维护性和一致性方面的优势就显着了。
#5
2
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.
似乎我在我的应用程序中使用绑定,KVO和数据源方法。这实际上取决于具体情况。例如,在我的一个项目中,除了主窗口的大纲视图之外,我几乎使用绑定,这很复杂,我甚至不想尝试将它装入NSTreeController。同时我还使用KVO重新加载UI对象并跟踪模型对象中的依赖项。
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.
在学习Bindings或Core Data等高级Cocoa主题时要记住的重要一点是,你必须了解它们背后的所有技术;从数据源协议,通知KVO等一切。一旦你有足够的经验与他们一起工作以了解“神奇”是如何工作的,你就能够轻松地将更高级别的东西集成到你的应用程序中。
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.
在您的特定情况下,您必须决定在开发应用程序的基础上学习绑定是否值得花费额外的时间。如果可能,使用绑定开发应用程序的简化原型可能会对您有所帮助,因此您知道在启动实际项目时如何最好地将各个部分组合在一起。
#6
2
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.
我的意见是,你应该采用绑定;现在,这项技术已被充分理解和稳定,并且值得为您不再需要编写的代码量做。当我第一次切换到绑定时,我在观察和观察对象的生命周期以及UI断裂时遇到了相当多的麻烦,因为它正在观察一个有效的对象,但是不正确的对象。一旦你看到这些问题几次,知道如何避免它们以及如果它们确实出现如何发现它们变得简单明了。伊什。我仍然希望“此事件在这里引起了此更新”在调试器中的痕迹,但我仍然很高兴我采取了行动。
#7
1
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.
我也发现布莱恩韦伯斯特的建议非常有用,因为我确实最终做了一些老式的方法,因为绑定不能做我想要的,或者因为做我需要的东西会非常复杂绑定。