指定要获取哪个对象的 super

发布于 2024-10-17 22:45:54 字数 501 浏览 2 评论 0 原文

就像标题所说,我想指定 NSArrayController 的超级,类似于 self = [super[NSArrayController] function],但没有运气搜索为了这。有什么想法吗?提前致谢。

编辑删除抽象示例,因为它们使人们对我的问题的性质感到困惑。

这样做的目的是以编程方式执行从 NSArrayController 到 NSButton 的“add”简单绑定在 IB 中执行的操作。我的应用程序中有多个 arrayController,因此我希望能够指定要通过代码获取哪个 arrayController。

我正在寻找 NSArrayController 的 super 的原因是因为我的印象是应该解决模型而不是控制器(NSArrayController),并且我的模型是一个核心数据模型,我相信我可以通过使用 super 来访问它我通过名称指定的 NSArrayController 的。也许有一种更直接的方法来添加到数据模型。

Like the titles says, I want to specify the super of an NSArrayController, something along the lines of self = [super[NSArrayController] function], but have had no luck searching for this. Any ideas? Thanks in advance.

Edited to remove abstract examples as they're confusing people as to the nature of my question.

The purpose of this is to programmatically do what a simple binding of 'add' from an NSArrayController to an NSButton would do in IB. There are several arrayControllers in my application so I want to be able to specify which one I want to obtain the super of by code.

The reason I am looking for the super of an NSArrayController is because I am under the impression that one should address the model rather than the controller (NSArrayController) and my model is a Core Data model that I believe I could get to by using the super of an NSArrayController I specify by name. Perhaps there is a more direct way of adding to the data model.

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

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

发布评论

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

评论(1

风和你 2024-10-24 22:45:54

你问了一个错误的问题。

首先,让我们区分和类的实例。请注意,同一个类可以存在并且确实经常存在多个实例。

C 可以是另一个类A子类。那么A 就是C超类。假设您有一个 C 类的实例 c。然后,在C类的方法的实现中,self代表c本身的实例,super代表code> 代表 c 的实例作为其超类 A 的实例。从某种意义上说,C 类的实例也是A 类的实例。

对象可以具有除超类或子类之外的其他关系。例如,类 C 可以在其接口中包含实例变量 B* b; 在这种情况下,类 c 的实例 < code>C 指向类B 的实例b 的指针。在这种情况下,c不是B的实例。

NSArrayController 和托管对象上下文之间的关系是后者之一。 NSArrayController 的实例包含指向 NSManagedObjectContext (moc) 实例的指针。

所以你想要做的不是获取你的NSArrayControllersuper。相反,您想要获取与 NSArrayController 关联的 moc。现在,你如何得到它?要找到它,您可以在 XCode 中或在 Apple Developer Connection 的 Web 上打开参考,参见此处。现在就这样做。通过这些方法。你找不到一个给你 moc 的人。

然后,您转到该页面的顶部,并遵循 NSArrayController 的超类。请参阅NSObjectController 的此引用。现在,浏览一下方法列表。您会找到 -[NSObjectController ManagedObjectContext],它就完成了这项工作!

总之:如果您想要与 NSArrayController 关联的 moc,您只需要执行以下操作

NSManagedObjectContext* moc= [arrayController managedObjectContext];

,其中 arrayController 是您想要的 NSArrayController 的实例处理。例如,如果笔尖中有多个 NSArrayController 实例,则应用程序委托中应该有多个 IBOutlet,例如 arrayController1,< code>arrayController2 等(这些都是非常糟糕的变量名)。然后你选择你想要处理的人。

You're asking a wrong question.

First, let's distinguish a class and an instance of the class. Note that there can be, and indeed often are, multiple instances of the same class.

A class C can be a subclass of another class A. Then A is the superclass of C. Suppose you have an instance c of the class C. Then, in the implementation of the methods of the class C, self stands for the instance of c itself, and super stands for the instance of c as an instance of its superclass A. In a sense, an instance of the class C is also an instance of the class A.

Objects can have other relationships than being super or subclasses. For example, a class C can have in its interface an instance variable B* b; In this case, an instance c of the class C has a pointer to an instance b of the class B. In this case, c is not an instance of the class B.

The relationship between NSArrayController and the managed object context is one of the latter. An instance of NSArrayController contains a pointer to an instance of NSManagedObjectContext (moc).

So what you want to do is not to get the super of your NSArrayController. Instead, you want to get the moc associated to the NSArrayController. Now, how do you get it? To find it out, you open the reference in XCode or on the web at the Apple Developer Connection, see here. Do that right now. Go through the methods. You don't find one giving you the moc.

Then, you go to the top of that page, and follow the superclass of NSArrayController. See this reference of NSObjectController. Now, go through the list of the methods. You find -[NSObjectController managedObjectContext], which does the job!

In conclusion: if you want the moc associated to the NSArrayController, you just need to do

NSManagedObjectContext* moc= [arrayController managedObjectContext];

where arrayController is the instance of the NSArrayController you want to deal with. e.g. If you have multiple instances of NSArrayControllers in the nib, you should have multiple IBOutlets in the app delegate, say, arrayController1, arrayController2, etc. (which are very bad variable names). Then you choose the one you want to deal with.

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