Suppose I have Objective C interface SomeClass which has a class method called someMethod:
#interface SomeClass : NSObject {
}
+ (id)someMethod;
#end
In some other interface I want to have a helper method that would dynamically invoke someMethod on a class like this:
[someOtherObject invokeSelector:#selector(someMethod) forClass:[SomeClass class];
What should be the implementation for invokeSelector? Is it possible at all?
- (void)invokeSelector:(SEL)aSelector forClass:(Class)aClass {
// ???
}
Instead of:
[someOtherObject invokeSelector:#selector(someMethod) forClass:[SomeClass class];
call:
[[SomeClass class] performSelector:#selector(someMethod)];
Example (using GNUstep ...)
file A.h
#import <Foundation/Foundation.h>
#interface A : NSObject {}
- (NSString *)description;
+ (NSString *)action;
#end
file A.m
#import <Foundation/Foundation.h>
#import "A.h"
#implementation A
- (NSString *)description
{
return [NSString stringWithString: #"A"];
}
+ (NSString *)action
{
return [NSString stringWithString:#"A::action"];
}
#end
Somewhere else:
A *a = [[A class] performSelector:#selector(action)];
NSLog(#"%#",a);
Output:
2009-11-22 23:32:41.974 abc[3200] A::action
nice explanation from http://www.cocoabuilder.com/archive/cocoa/197631-how-do-classes-respond-to-performselector.html:
"In Objective-C, a class object gets all the instance methods of the
root class for its hierarchy. This means that every class object
that descends from NSObject gets all of NSObject's instance methods -
including performSelector:."
In Objective-C, classes are objects as well. The class objects are treated differently, however, as they can call the instance methods of their root class (NSObject or NSProxy in Cocoa).
So it's possible to use all the instance methods defined in NSObject on class objects as well and the right way to dynamically invoke a class method is:
[aClass performSelector:#selector(aSelector)];
The apple docs are a bit more specific.
You shouldn't implement this yourself.
The NSObject Protocol has a performSelector: method that does exactly this.
Is this built-in method what you want?
id objc_msgSend(id theReceiver, SEL theSelector, ...)
(See the runtime reference docs for this function.)
Related
Let's say I have class A and class B. In class A I create an instance of class B. Class B has delegate methods that, when called, I want to call instance methods in class A. Is there a way to pass a reference to class A when instantiating class B?
You can use a delegate to return information to the first Class
Class B.h
#protocol delegateB <NSObject>
-(void)doSomething:(NSString *)data;
#end
#interface ClassB : UIViewController
#property(nonatomic, assign) id<delegateB> delegate;
#end
Class B.m
#synthesize delegate = _delegate;
- (void)viewDidLoad {
[_delegate doSomething:#"String to send"];
}
In the class A:
Class A.h
#interface ClassA : UIViewController
-(void)doSomething:(NSString *)data;
#end
Class A.m
When you instance the class, you need to assign self:
ClassB *cb = [[ClassB alloc] init];
cb.delegate = self;
To use the function:
-(void)doSomething:(NSString *)data{
//do something whit the data
}
Add a property of type A to B and assign self to it after initialising. You could also pass self as an argument to the delegate method you mentioned. Actually this is common practice in cocoa (look at UITableViewDelegate)
You can use type Class to pass a reference to a class. For class A use [A class] to get the referenece. But note that with a general class reference you won't be able to simply write [aClassRef aClassMethod], because Class instancce can reference any class (so the compiler don't know if particular method is supported or not).
The actual solution should be closer to what Julian suggests, but maybe this note will be useful in some way too.
The description method is a class method of the NSObject class. I guess it's a class method, because NSObject cannot be initialized.
When I do this:
NSLog(#"%#", [NSObject description]);
It prints out:
NSObject
But when I create a class that directly inherits from NSObject, and do this:
MyNewClass *obj = [[MyNewClass alloc] init];
NSLog(#"%#", obj);
This prints out something like:
<MyNewClass: 0x4b234a0>
I didn't specifically override the description method, how did it get overriden by my new class?
That is because NSObject has two methods:
+ (NSString *)description; // Class method
- (NSString *)description; // Instance method
Former is defined in NSObject class and later one in in NSObject Protocol.
I google this question and spend some time to figure it out by myself but with a bad luck.
I need to call class's static method which is hidden for class's user.
// MyClass.h
#interface MyClass : NSObject
#end
// MyClass.m
#implementation MyClass
- (NSString *)myInstanceMethod
{
return #"result string";
}
+ (NSString *)myStaticMethod
{
return #"result string";
}
#end
------------------------------------------------------------
// MyCallerClass.m
#import "MyClass.h"
#implementation MyCallerClass
- (void) testMethod
{
MyClass *inst = [MyClass new];
// call 1
NSString *resultInstance = [inst performSelector:#selector(myInstanceMethod)];
// call 2
NSString *resultStaitc = [inst performSelector:#selector(myStaticMethod)];
// call
[MyClass myStaticMethod];
}
#end
Call 1 works good, Call 2 returns nil, Call 3 does not compile.
How can I call static method which does not defined in .h file and give correct returned object?
Thank in advance,
Rost
For Call 2 ,
since it is an class method you should call like
NSString *resultStaitc = [[inst class] performSelector:#selector(myStaticMethod)];
inst is the object.To call a class method you must call with class.
The object instance's class is supposed to be calling the method, not the instance itself.
For call 3
It should be working fine,The result value is never used .the compile error is because
+ (NSString *)myStaticMethod;
not declared in .h
use
NSString *resultStaitc1 =[MyClass myStaticMethod];
and it will return the value to the resultStaitc1
Another option is to declare an informal protocol for MyClass at the top of MyCallerClass.m. An informal protocol is just a category interface without the implementation block. You can stick you method declaration(s) in there. It does raise synchronisation problems between the two source files, but so does performSelector:. Doing it this way lets you call methods that have a different signature to just take [0-2] object arguments and return and object.
What is the difference of using:
+ (id) myMethod;
// Rather than
- (id) myMethod;
Using a + declares the method as a class method, or a method that can be called directly on the class, where the class is the object. So when you have this:
#implementation Foo
+(NSString*)method1 {
return #"Foo";
}
-(NSString*)method2 {
return #"Foo";
}
#end
The methods are called in different ways:
[Foo method1]; //=> #"Foo"
Foo* f=[[Foo alloc] init];
[f method2]; //=> #"Foo"
One other thing to note is that class methods don't have access to an instance, which means they can't access any kind of instance variables.
#Linuxios pretty much summed up the concept of class and instance method. However, since you mentioned getters and setters in your title, I want to point out that in Objective-C you can use properties instead of writing your own accessor methods. For example,
In the header file, you will have something like this:
#interface MyObject : NSObject
#property (nonatomic,retain) NSSet* mySet;
#end
In the m file, you wil have something like this:
#implement MyObject
#synthesize mySet;
#end
To access the set in another class you can do it like this:
myObject.mySet; // assuming myObject is an instance of the MyObject class
The top one is a class method (no instance required)
The second one is a instance variable (attached to a specific instance).
This answer explains the methods quite well:
Method Syntax in Objective C
[MyObject myMethod]; // did not have to create an instance
MyObject* myNewObject = [[MyObject alloc] init] autorelease];
[myNewObject myMethod]; // had to create an instance
I need to move the same method from 4 different classes to the superclass.
Such methods are exactly the same except for the type of a variable declared in them:
For example, in the method in the first class I have
FirstClass var = [[FirstClass alloc] init]
in the second class
SecondClass var = [[SecondClass alloc] init]
and so on.
What's the best way to implement this variation in the superclass ?
Should I use NSClassFromString in the superclass and get each string from each method in the subclasses?
thanks
I'm not 100% sure I get what you mean. So I could be answering the wrong question
If inside your class you need to use an object (I've called it worker below) to do your work, but the class of this object is not known til later, you can use dependency injection (DI).
MyClass.h
#interface MyClass : NSObject
#property (nonatomic, retain) id<WorkerInterface> worker;
#end
MyClass.m
#implementation MyClass
#synthesize worker = _worker;
- (void)myMethod;
{
[self.worker doSomething];
}
// You could also provide a default class to use if one is not passed in
//
// - (id<WorkerInterface)worker;
// {
// if (!_worker) {
// _worker = [[DefaultWorker alloc] init];
// }
// return _worker;
// }
#end
Now whenever I instantiate this class I can simply pass in the appropriate object to be used e.g:
MyWorkerClass *worker = [[MyWorkerClass alloc] init]; // <- Conforms to #protocol(WorkerInterface)
MyClass *instance = [[MyClass alloc] init];
instance.worker = worker;
[instance doSomething];
If all the different types of iVar's you intend on initializing in the subclasses are descended from a common class, then I'd store that class in the super, or else just store it as an id. Then, setup a property accessor in each of your subclasses the casts the iVar as you need it.
#interface superClass : NSObject{
id _superIvar;
}
#end
#implementation superClass : NSObject
....super's code....
#end
Now in the implementation of the subclass declare a property in a category, shown below (or in the interface, if you want it public)
#interface subClass (private)
#property (strong) ClassType *superIvar;
#end;
#implementation
- (void) setSuperIvar:(ClassType *)superIvar{
_superIvar = superIvar;
}
- (ClassType *) superIvar{
return (ClassType *) _superIvar;
}
- (void) someMethodThatUsesSuperIvar{
[self.superIvar doSomething];
}
#end
Alternatively, if you don't want to open your _superIvar to direct access, you can set a property on the superclass and access through the property on the subclass. But in this way you can easily access super's ivars cast to the appropriate type.