1
votes

I have two protocols BaseDAO, and ICategoryDAO. ICategoryDAO adopt BaseDao. Class categoryDao adopt ICategoryDao.

The below code is the initializer method of serviceRepo class (sub class of NSObject). Here I perform the type check and etc.

 - (id)init {
        self = [super init];
        if (self) {
            self.dict  = [NSMutableDictionary new];
            [self.dict setValue:[CategoryDao description] forKey:[NSString stringWithFormat:@"%s",[@protocol(ICategoryDao) name]]];
        }
        return self;
    }

the problem is Xcode always pop the error Receiver type 'Protocol' for instance message is a forward declaration in this call -> [@protocol(ICategoryDao) name]. Im using ARC, it works fine on non ARC project. I couldn't find any solution in the internet, so finally Im here. How can I solve this issue, am I doing anything wrong here.

This is the structure of the protocols and classes;

BaseDao

#import <Foundation/Foundation.h>

@protocol BaseDao <NSObject>

ICategoryDAO

#import <Foundation/Foundation.h>
#import "BaseDao.h"

@protocol ICategoryDao <BaseDao>

CategoryDAO

#import <Foundation/Foundation.h>
#import "ICategoryDao.h"

@interface CategoryDao : NSObject <ICategoryDao>

ServiceRepo

#import <Foundation/Foundation.h>

@interface ServiceRepository : NSObject

ServiceRepo.m

#import "ServiceRepository.h"
#import "CategoryDao.h"
#import "BaseDao.h"

@implementation ServiceRepository
1
did you import the file "MockCategoryDao" - Manish Agrawal
Usually an error like this means, that you need to import the .h file. So you could try importing the ICategoryDao.h directly in the SeviceRepo.m. Maybe that helps. - Tobi
Out of curiosity i've never seen sth. like [@protocol(ICategoryDao) name]. What does the @protocol() do here? - Tobi
[@protocol(ICategoryDao) name] name is the method of iCategoryDao protocol right? - Pandey_Laxman
i just want to store the name of the protocol as the key in the dictionary. @Pandey no it is not. name method return nsstring object used to uniquely identify the receiver . is there any other way, I can use to store the name of the protocol as key in the dict. - Clement Prem

1 Answers

1
votes

Please try below code that might help you.

[self.dict setValue:[CategoryDao description] forKey:[NSString stringWithFormat:@"%s",NSStringFromProtocol(@protocol(ICategoryDao))];

NSProtocolFromString Returns a the protocol with a given name.

Protocol *NSProtocolFromString (
   NSString *namestr
);

Parameters

namestr The name of a protocol.

Return Value

The protocol object named by namestr, or nil if no protocol by that name is currently loaded. If namestr is nil, returns nil.