#import "MPOContactAuthorizationManager.h"
@protocol MPOContactAuthorizationManagerDelegate <NSObject>
- (void)authorizationManger:(MPOContactAuthorizationManager *)manager
didUpdateContactState:(ContactsState)contactState;
@end
MPOContactAuthorizationManager
和ContactState
即使在MPOContactAuthorizationManager中声明它们也不会解析为类型:
#import "MPOContactAuthorizationManagerDelegate.h"
typedef enum _contactsState {
kContactsStateUnknown,
kContactsStateAllowed,
kContactsStateDisallowed
} ContactsState;
@interface MPOContactAuthorizationManager : NSObject <UIAlertViewDelegate> {
ContactsState _contactsAuthorizationState;;
}
@property (strong, nonatomic) NSObject<MPOContactAuthorizationManagerDelegate> *delegate;
@property (nonatomic) ContactsState contactsAuthorizationState;
关于为什么这些没有解决的任何想法?两者都收到错误“预期类型”
由于 麦克
答案 0 :(得分:3)
你有一个循环依赖。通过删除MPOContactAuthorizationManagerDelegate.h
行并添加以下内容来更新#import
标题:
@class MPOContactAuthorizationManager;
在@protocol
行之前。
只需将两者放在一个.h文件中(您仍然需要MPOContactAuthorizationManager
的前向声明):
typedef enum _contactsState {
kContactsStateUnknown,
kContactsStateAllowed,
kContactsStateDisallowed
} ContactsState;
@class MPOContactAuthorizationManager;
@protocol MPOContactAuthorizationManagerDelegate <NSObject>
- (void)authorizationManger:(MPOContactAuthorizationManager *)manager
didUpdateContactState:(ContactsState)contactState;
@end
@interface MPOContactAuthorizationManager : NSObject <UIAlertViewDelegate> {
ContactsState _contactsAuthorizationState;;
}
@property (strong, nonatomic) NSObject<MPOContactAuthorizationManagerDelegate> *delegate;
@property (nonatomic) ContactsState contactsAuthorizationState;
答案 1 :(得分:1)
使用协议的前向声明替换MPContactAuthorizationManager.h中“MPOContactAuthorizationManagerDelegate.h”的导入:
@protocol MPOContactAuthorizationManagerDelegate;
typedef enum _contactsState {
kContactsStateUnknown,
kContactsStateAllowed,
kContactsStateDisallowed
} ContactsState;
@interface MPOContactAuthorizationManager : NSObject <UIAlertViewDelegate> {
ContactsState _contactsAuthorizationState;;
}
........
您需要选择此导入方案,因为您在协议标题中引用了用户定义的类型(ContactsState
类型) - 因此此标头需要导入管理器的标头。但是,在管理器的标题中,您只将协议称为方法参数的类型,因此您可以合法地在此处声明。
通常,在少数情况下,您只应从另一个标题中导入一个标题:
typedef
),在其他标题中定义另外 - 阅读this enlightening answer。
在公共方法参数和属性中需要协议和类名的更常见情况可以使用@class
和@protocol
的前向声明来完成。虽然,如果是我,我会将协议声明保留在与授权管理器相同的标题中 - 这样看起来更方便。请注意,您需要在文件中为此执行前向声明。例如:
typedef enum _contactsState {
kContactsStateUnknown,
kContactsStateAllowed,
kContactsStateDisallowed
} ContactsState;
//forward-declare the protocol before referencing it in the file
@protocol MPOContactAuthorizationManager;
@interface MPOContactAuthorizationManager : NSObject <UIAlertViewDelegate> {
ContactsState _contactsAuthorizationState;;
}
@property (strong, nonatomic) NSObject<MPOContactAuthorizationManagerDelegate> *delegate;
@property (nonatomic) ContactsState contactsAuthorizationState;
........
@end
// Provide the real protocol declaration.
@protocol MPOContactAuthorizationManagerDelegate <NSObject>
- (void)authorizationManger:(MPOContactAuthorizationManager *)manager
didUpdateContactState:(ContactsState)contactState;
@end