rtovars
rtovars

Reputation: 440

Inconsistent 'Unknown type name' error in protocol referencing NSManagedObject subclasses

I have two protocols declared in my project. The first one I wrote is named SSSelectingCategory.h

#import <Foundation/Foundation.h>

@protocol SSSelectingCategory <NSObject>

@required
@property (nonatomic, strong) SSCategory *selectedCategory;

@end

The second one is SSSelectingIcon.h

#import <Foundation/Foundation.h>

@protocol SSSelectingIcon <NSObject>

@required
@property (nonatomic, strong) SSIcon *selectedIcon;

@end

The strange thing is that the first protocol compiles without any error or warning, while the second one is throwing the Unknown type name 'SSIcon'.

I know the answer to this problem is to use a forward-class declaration in my second protocol like this:

#import <Foundation/Foundation.h>
@class SSIcon;

@protocol SSSelectingIcon <NSObject>

@required
@property (nonatomic, strong) SSIcon *selectedIcon;

@end

But now this makes me think the SSSelectingCategory.h protocol shouldn't have compiled at all in the first place, as I didn't put any explicit #import or forward-class declaration of any sort.

I could quiet the compiler and go on coding, but I want to understand if there's something weird going on with Xcode or if I'm missing some fine point, as both protocols are really simple. Could there be a problem with the SSCategory and/or SSIcon classes, as they're both NSManagedObjects and there's a relationship connecting the two?

Upvotes: 0

Views: 1638

Answers (1)

Martin R
Martin R

Reputation: 539915

The compiler compiles the .m files, which in turn include the .h files.

So I assume that in the .m file where "SSSelectingCategory.h" is included, SSCategory is defined before, so that the compiler has no problem compiling the protocol.

If, on the other hand, "SSSelectingIcon.h" is included without having defined SSIcon before, the compiler would abort with an error there.

In any case, it makes sense to include the required interfaces in the .h file (or forward declare the required classes), because that enables code-completion in Xcode.

Upvotes: 1

Related Questions