Specifying which object to get the super of
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开发者_JAVA百科'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.
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 NSArrayController
s in the nib, you should have multiple IBOutlet
s in the app delegate, say, arrayController1
, arrayController2
, etc. (which are very bad variable names). Then you choose the one you want to deal with.
精彩评论