Reputation: 107211
Currently I'm working on an iOS application in which I'm using delegates and I implemented something like:
@protocol Hello <NSObject>
@required
- (void)update:(NSDictionary *)data;
@end
@interface NotificationHandler : NSObject
{
id <Hello>delegate;
}
- (void)sendData;
@end
When some specific notification occurs it will call the sendData
method and it'll call the update
method of delegate
.
Everything is working fine. In the dictionary I need to pass specific values for some special delegates. So I'm importing the headers like:
#import "Special1.h"
#import "Special2.h"
- (void)sendData
{
if([_delegate isKindOfClass:[Special1 class]])
{
//special class 1
NSDictionary *dict = //initialize with parameters and values
[(Special1 *)_delegate update:dict];
}
else if([_delegate isKindOfClass:[Special2 class]])
{
//special class 2
NSDictionary *dict = //initialize with parameters and values
[(Special2 *)_delegate update:dict];
}
}
Here the issue is, I need to import all the special classes here. Is there anyway to do this without importing the class ?
Upvotes: 1
Views: 1045
Reputation: 107211
I found a solution using NSClassFromString like:
- (void)sendData
{
if([_delegate isKindOfClass:NSClassFromString(@"Special1")])
{
//special class 1
NSDictionary *dict = //initialize with parameters and values
[_delegate performSelector:@selector(update:) withObject:dict];
}
else if([_delegate isKindOfClass:NSClassFromString(@"Special2")])
{
//special class 2
NSDictionary *dict = //initialize with parameters and values
[_delegate performSelector:@selector(update:) withObject:dict];
}
}
Upvotes: 1