In Objective-C, when are the class pairs - metaclass object and class object, created? Is it in the beginning of the app execution or at the point when you first instantiate the first object of a class?
At the point that the class is added to the class hierarchy. This is generally when the app is launched, but could be later if the class is added by a dynamically loaded library or is created programatically.
The class object and metaclass have to exist at the time that +load is called (which is the above).
Related
In my class I'm referencing a utils object that holds some consts. That utils object inherits from a parent utils object that also has consts. In my class, I want to access the parent's companion consts via a reference to the child utils class. Is this possible?
EDIT
This isn't technically necessary (simply referencing the base class works in my specific case) but I'm still interested from a language perspective if this is possible.
Companion objects and their members can only be accessed via the containing class name, not via instances of the containing class. [...] If you try to redeclare a companion object in a subclass, you'll just shadow the one from the base class.
In other words: It's not possible, as both companion objects are completely unrelated.
#import <anthercls.h>
+ (void)load
{
[anothercls sharedInstance];
}
What puzzles me is when the class's load message is called, does another class is loaded in memory? How to ensure that another class was loaded before the class. because of #import?
I have searched for a while,but can't get a satisfying answer.
You can't know the order in which +load is called, with exceptions: when+load is called on a class in your app there are classes that are guaranteed to have been already loaded:
classes from frameworks you link to
superclass of the class +load is currently executed
So if anothercls is a class from a framework you link to, it's safe to use it. More about +load and +initialize here
my situation:
a dashboard controller who register the widgets inside it and automatically builds the view.
widget classes that are going to populate the dashboard, who adopt <widget> protocol(some informal methods required), and need to be registered in the dashboard class so the dashboard singleton knows who wants to be in.
the dashboard class has +(void)register:(Class<widget>)w; that simply register the classes who wants to be in in an NSArray
I need each widget class to call automatically that method.in c++ i used to make a boolean static variable that i would initialize with that method.
Language = objective-c
The objective-c runtime will call two methods when a class is first loaded. +load and +initialize. I believe what you want could be done by calling [self register] from within +initialize.
A way you could do it is with the runtime:
Grab a list of all the classes known to the runtime.
Iterate the list, and check to see if the class conforms to your widget protocol
If it does conform to the protocol, invoke the +register: method or whatever
Regarding step #2, you can't use the +conformsToProtocol: method, because one of the classes you'll iterate is the NSZombie class, which triggers an exception whenever you invoke a method on it. Thus, you'd want to use the class_conformsToProtocol() runtime function instead.
I have a class that is derived of UITableViewController and handles everything related with a specific type of tables. Let's call it Class T
In may main application class, Class A, I have methods to populate other areas of the screen as, for instance, a map.
While I'm populating my table within Class T, I would like to call the Class A method that plots the x,y points on the map.
Is this possible? What should be the correct way to do this?
When I though about this approach, I was expecting that invoking [super ...] inside Class T would call the Class A methods, as this is the owner class of the instance, but ofcourse it call the parent class, in my case the UITableViewController.
Thank you,
Pedro
If A is your main application class, you should be able to access the application instance with [UIApplication sharedApplication], cast it to the class A type, and call the APIs you need to call.
Why not define a ClassAProtocol and then add a property "classADelegate" in Class T?
ClassAProtocol will define a method like:
-(void)plotXYOnMapFromData:(id)someObjectContainingDataToPlot;
So in the Class T interface you will add:
#property (assign) id classADelegate;
and then when you instantiate, let's say from instanceA (instance of Class A), instanceT (instance of Class T) you will do:
instanceT.classADelegate = instanceA;
Finally inside Class T you can call the plotting method in this way:
[classADelegate plotXYOnMapFromData:myDataToPlot];
The advantage of the delegate pattern in this case is that Class T just need to know only one small piece of ClassA, which is the protocol, and ClassA is able to communicate with T thanks to its implementation of the protocol.
I have a questions about the iPhone application. I am the green of the iPhone application. When I read the document(PDF) download from the apple developer website (online version). I found that the document always mentions different methods of the library.
There are
1) Class method
2) Instance method
3) Delegate method
I understand the use and meaning of the instance method, which is called by a instance.
let's say the delegate methods is the connection:didReceiveAuthenticationChallenge and the class method sendSynchronousRequest:retruningResponse:error:.
However, I don't understand about the different between the class method and the delegate method. Is the class method for the whole class? or whole project? What it means of the delegate? and where should I put the code after I modify the content of the delegate? How can I call the method?
Can anyone help me. Thank you very much.
It is another question about the delegate method. And I don't how to solve the problems. Please help me. Thank you.
HTTP status code = 0 (iPhone) (objective c)
Suppose you have a class Foo and an instance of that, Foo* foo.
Then, the class method is a method which is sent to the class:
[Foo classMethod];
while the instance method is a method sent to the instance:
[foo instanceMethod];
The delegate method is a method which the instance of the class calls. So, you typically implement another class Delegate with an instance Delegate* delegate, and do
[foo setDelegate:delegate];
Then, the object foo calls the delegate method of delegate at appropriate times:
[delegate delegateMethod];
This is a way to receive an event from the system API.
Apple provides extensive documentation on the fundamentals for Objective-C and Cocoa - if in doubt, this should be your first stop.
The Objective-C Programming Language - Class Objects:
[...] a class definition can include methods intended specifically for the class object—class methods as opposed to instance methods. A class object inherits class methods from the classes above it in the hierarchy, just as instances inherit instance methods.
Cocoa Fundamentals Guide - Delegates and Data Sources:
A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program.
The delegating object is often a responder object—that is, an object inheriting from NSResponder in Application Kit or UIResponder in UIKit — that is responding to a user event. The delegate is an object that is delegated control of the user interface for that event, or is at least asked to interpret the event in an application-specific manner.
And some related background in The Objective-C Programming Language - Protocols:
Class and category interfaces declare methods that are associated with a particular class — mainly methods that the class implements. Informal and formal protocols, on the other hand, declare methods that are independent of any specific class, but which any class, and perhaps many classes, might implement.
A delegate method is a method that is defined in a classes delegate protocol. They are added to your class but your class must have the objects delegate protocol. They are usually used by the object but is something that you must define for the object. NSTableView and UITableView use delegate methods to populate their data. A class method is just one that you define in your interface.