Objective-C Cannot find interface declaration

importinterfaceobjective c

I have been scratching my head for awhile trying to figure this one out.

I have the following .h files

//ClassA.h
#import "ClassB.h"

@interface ClassA {
}

@property SomeEnum blah;

@end

//ClassB.h
#import "ClassA.h"

typedef enum SomeEnum
{
     value1,
     value2
}SomeEnum;

@interface ClassB {
}

@property SomeEnum otherblah;

@end

@interface ClassA (Category) {
}

@end

I think the problem is that ClassA needs SomeEnum from ClassB and so it needs to import it, and ClassB needs ClassA for its category so it needs to import it. Is there a problem with them importing each other? Do I need to include a third class?

Here are the compiler errors I'm getting:

In ClassB.h: Cannot find interface declaration for 'ClassA'
In ClassA.h: Unknown type name 'SomeEnum'

EDIT: I got it to work by moving the category declaration into ClassA and then having an @class ClassA in ClassB.h…but I'm still not sure why it wouldn't work in the first place

Best Answer

Do you have (non-category declaration):

@interface ClassB

@end

declared anywhere, usually in ClassB.h?
Or a forward declaration:

@class ClassB

in ClassA.h?


Try this for your ClassA.h:

//ClassA.h

@class ClassB

@interface ClassB (Category)
+ (id)classMethod
@end

Then put #import "ClassB.h" in ClassA.m.


You should put this in ClassA.h:

@interface ClassA (Category) {
}

@end

And then the:

@implementation ClassA (Category)

in ClassA.m above the non-category @implementation ClassA.


Personally, I create seperate files for categories. For example, this is in UIView-Extended.h:

@interface UIView (UIView_Extended)

enum {
    UIViewAutoresizingHorizontal = (UIViewAutoresizingFlexibleLeftMargin | 
                                    UIViewAutoresizingFlexibleWidth | 
                                    UIViewAutoresizingFlexibleRightMargin),
    UIViewAutoresizingVertical = (UIViewAutoresizingFlexibleTopMargin | 
                                  UIViewAutoresizingFlexibleHeight | 
                                  UIViewAutoresizingFlexibleBottomMargin),
    UIViewAutoresizingAll = (UIViewAutoresizingFlexibleLeftMargin | 
                             UIViewAutoresizingFlexibleWidth | 
                             UIViewAutoresizingFlexibleRightMargin | 
                             UIViewAutoresizingFlexibleTopMargin | 
                             UIViewAutoresizingFlexibleHeight | 
                             UIViewAutoresizingFlexibleBottomMargin)
};
typedef NSUInteger UIViewAutoresizing;

- (UIView *)findFirstResponder;
- (UIView *)setFirstResponder:(NSUInteger)viewTag;
- (IBAction)dismissKeyboard:(id)sender;
- (IBAction)nextPrevious:(id)sender;

@end

And then in UIView-Extended.m:

#import "UIView-Extended.h"

@implementation UIView (UIView_Extended)

- (UIView *)findFirstResponder {
    if ([self isFirstResponder]) {
        return self;
    }

    for (UIView *subview in [self subviews]) {
        UIView *firstResponder = [subview findFirstResponder];
        if (firstResponder) {
            return firstResponder;
        }
    }
    return nil;
}

- (UIView *)setFirstResponder:(NSUInteger)viewTag {
    if (self.tag == viewTag) {
        [self becomeFirstResponder];
        return self;
    }
    for (UIView *subview in self.subviews) {
        UIView *v = [subview setFirstResponder:viewTag];
        if (v) {
            return v;
        }
    }
    return nil;
}

- (IBAction)dismissKeyboard:(id)sender {
    [[self findFirstResponder] resignFirstResponder];
}

- (IBAction)nextPrevious:(id)sender {
    UIView *responder = [self findFirstResponder];  
    if (!responder) return;
    NSInteger newTag;
    NSInteger tagMod = 1;
    if (sender) {
        tagMod = (((UISegmentedControl *)sender).selectedSegmentIndex ? 1 : -1);
    }
    UIView *v = [self viewWithTag:responder.tag + tagMod];
    if ([v isKindOfClass:[UITextField class]] || [v isKindOfClass:[UITextView class]]) {
        newTag = responder.tag + tagMod;
        [self setFirstResponder:newTag];
    } else {
        //  do something else... but what??
    }
}

@end

Then, in my Prefix file (Project-Prefix.pch):

#import "UIView-Extended.h"