指定要获取其上级的对象 [英] Specifying which object to get the super of

查看:56
本文介绍了指定要获取其上级的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像标题中所说的,我想指定 NSArrayController 的超级名称,类似于 self = [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.

此操作的目的是以编程方式执行从NSArrayController到NSButton的"add"的简单绑定,这将在IB中完成.我的应用程序中有几个arrayControllers,所以我希望能够指定要通过代码获取其上一个的超级控制器.

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.

我之所以要寻找一种NSArrayController的超级版本,是因为我觉得应该解决模型而不是控制器(NSArrayController),并且我的模型是一个Core Data模型,我相信我可以通过使用我通过名称指定的NSArrayController的超级.也许有一种更直接的方法可以添加到数据模型中.

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.

推荐答案

您在问一个错误的问题.

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.

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

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.

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

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.

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

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).

因此,您要做的就是不要获取 NSArrayController super .相反,您想获取与 NSArrayController 相关的moc.现在,你如何得到它?要找到它,您可以使用XCode或在Web上的Apple Developer Connection上打开引用,

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.

然后,转到该页面的顶部,并遵循 NSArrayController 的超类.参见 NSObjectController 的此引用.现在,浏览方法列表.您会找到-[[NSObjectControllermanagedObjectContext] ,可以完成工作!

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!

结论:如果您想将Moc与 NSArrayController 相关联,则只需要做

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

NSManagedObjectContext* moc= [arrayController managedObjectContext];

其中 arrayController 是您要处理的 NSArrayController 的实例.例如如果笔尖中有多个 NSArrayController 的实例,则应用程序委托中应具有多个 IBOutlet ,例如 arrayController1 arrayController2 等(这是非常糟糕的变量名).然后,选择要处理的那个.

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.

这篇关于指定要获取其上级的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆