扩展 NSMutableArray 以进行双向关联管理

发布于 2024-09-08 18:06:12 字数 861 浏览 3 评论 0原文

我是代码生成(来自 UML)的忠实粉丝,并且来自 Java 世界,我想知道如何在 Objective-C 中实现自动双向关联管理。

想象一个协会合作伙伴<->地址、一对多且可从两端导航。我想要实现的是,如果我将地址附加到合作伙伴,则该地址对象应该自动了解其合作伙伴。

因此,实现模式是在 Partner 端有一个 NSMutableArray*,在 Address 端有一个 Partner*。地址一侧的属性很容易实现,因为 setPartner:(Partner*)aPartner 可以自动将地址(自身)插入到管理地址的合作伙伴的 NSMutableArray 中。然而另一面却并不那么容易实现。 Objective-C 中多引用的标准实现模式似乎是通过 @property 的 get 方法获得的 NSMutableArray。然后,拥有此 NSMutableArray 的对象可以将一个 Address 对象插入到该数组中,这当然不会更新另一侧。

我知道这种关联管理还有其他模式,例如通过 addTo...() 和 removeFrom...() 方法。但我还不知道这是否符合 Cocoa 编程的其他原则,甚至是否会妨碍我有效地使用 Cocoa。我在这里考虑 Interface Builder。经验不多,但我见过一种叫做 ArrayController 的东西,它似乎很方便,但似乎也期望使用 NSMutableArray 类型属性。如果这个人将对象插入到数组中,我需要拦截它并进行另一侧调整。

作为一名 Java 程序员,我现在倾向于继承 NSMutableArray 的子类并重写它的一些方法,然后可以操纵另一端。这可能吗?我读过有关类别的内容,但到目前为止我已经明白,我只能以这种方式向类添加方法,而不能覆盖它们,也不能添加到其结构中。或者应该是方法转发?我现在很困惑。如果你能指出我正确的思考方向那就太好了。多谢!

I am a great fan of code generation (from UML) and coming from the Java world, I wonder how I would implement automated bi-directional association management in Objective-C.

Image an association Partner <-> Address, one-to-many and navigable from both ends. What I would like to achieve is that if I append an Address to a Partner that Address object should automatically know about its Partner.

So the implementation pattern would be to have an NSMutableArray* on the Partner side and a Partner* on the Address side. The property on the Address side is easy to implement, as a setPartner:(Partner*)aPartner could automatically insert the Address (self) into the Partner's NSMutableArray managing the addresses. The other side, however, is not so easy to implement. The standard implementation pattern for to-many references in Objective-C seems to be the NSMutableArray obtainable via the get method of the @property. The object in possession of this NSMutableArray could then insert an Address object into the array, which would of course not be updating the other side.

I know that there are other patterns for this kind of association management, for instance, via addTo...() and removeFrom...() methods. But I don't know yet if this would fit with other principles of Cocoa programming or even prevent me from using Cocoa efficiently. I am thinking about Interface Builder here. Not much experience, but I have seen something called an ArrayController which seems to be quite handy but which also seems to expect an NSMutableArray type property to work with. And if this guy inserts objects into the array I need to intercept that and make the other side adjustment.

As a Java programmer I would tend to subclass NSMutableArray now and override some of its methods which could then manipulate the other end. Would this be possible at all? I read about categories but so far I have understood that I could only add methods to a class this way and not override them nor add to the structure of it. Or should it be method forwarding? I am confused right now. If you could point me into the right direction of thinking it would be so great. Thanks a lot!

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

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

发布评论

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

评论(1

始终不够 2024-09-15 18:06:12

欢迎来到可可。不要对内置集合进行子类化。你会发疯的。

请允许我澄清一下:在 Cocoa 中,我们有这些名为 "类簇"。集群是私有类的层次结构,所有类都有一个公共的公共超类。在本例中,NSArray 是公共超类,并且有一些私有子类是实际的数组实现。这在子类化时提出了一个非常困难的挑战,因为您不知道需要子类化哪个类(或多个类)。

常见的解决方法是创建一个新的 NSObject 子类来“包装”一个 NSArray (即,它具有作为实例变量 [“field”] 的数组),然后您调用自定义包装器上的方法,包装器将保存您需要的所有自定义逻辑。

至于回答你的问题,我发现当我进行这种设置时,我需要自动维护一对一、一对多或多对多关系,没有什么比使用核心数据。 CoreData 是一个内置框架,或多或少像一个对象存储。它所做的真正令人敬畏的事情之一是处理关系完整性,这正是您所寻找的。

Welcome to Cocoa. Do not subclass built-in collections. You will go insane.

Allow me to clarify: in Cocoa, we have these things called "Class Clusters". Clusters are a hierarchy of private classes that all have a common, public superclass. In this case, NSArray is the public superclass, and there are private subclasses that are the actual array implementations. This presents a really difficult challenge when subclassing, because you don't know what class (or classes) you would need to subclass.

The common work-around is to create a new NSObject subclass that "wraps" an NSArray (ie, it has the array as an instance variable ["field"]), and then you invoke methods on the custom wrapper, and the wrapper holds all the custom logic you need.

As to answer your question, I've found that when I have this sort of set up where I need to maintain one-to-one, one-to-many, or many-to-many relationships automatically, there's nothing that beats using CoreData. CoreData is a built-in framework that's more or less like an object store. One of the truly awesome things that it does is handle relationship integrity, which is what you're looking for.

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