0
votes

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

3
Is that the entire ClassB.h file? Is it empty except for the import statement (and comments)? - chown
No it's not... I will edit my post to include more relevant info - JPC
Is @interface ClassA(Category) actually supposed to be within ClassB? I think that is not legal objective C. - ThomasW
I can definitely declare categories for other classes. I'm pretty sure at least... It's done other places in my code - JPC

3 Answers

2
votes

You'll want to break out your dependencies. Put the definition of enum SomeEnum in one header file, then include an import for that header in ClassA.h and ClassB.h. In ClassA.h remove the import of ClassB.h.

2
votes

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"
0
votes
#import <UIKit/UIKit.h>

you should import <UIKit/UIKit.h> in any object contains "interface declaration"

e.g: UIImage NSDate etc..