PDA

View Full Version : Obj-C Category methods not found




GregX999
May 10, 2009, 12:20 PM
I'm just trying out implementing a category in my Obj-C project and I'm having a small glitch.

I have a class, Map, with a .h and .m file.
I have a class Map+LineOfSight that is a Category.

Here's the code in Map+LineOfSight.h:

#import "Map.h"

@interface Map ( LineOfSight )

- (NSDictionary *)visibleHexesFrom:(Hex *)hex withinRange:(int)range;

@end
(There are some other methods declared, but they're not relevant.)

And in Map+LineOfSight.m:

#import "Map+LineOfSight.h"
#import "Hex.h"

@implementation Map ( LineOfSight )

- (NSDictionary *)visibleHexesFrom:(Hex *)hex withinRange:(int)range
{
...do stuff...
return visibleHexes;
}

@end
(Again, the other methods have been left out.)

Then, in another class, "MapView", I import Map.h:
#import "Map.h"

Create a pointer to the Map object that was created by yet another class, "GameData".

- (void)prepareForNewMap
{
map = gameData.map;
...do some other stuff...
}

And finally, I have this method:

- (void)setHexYouAreHere:(Hex *)hex;
{
hexYouAreHere = hex;
[visibleHexes removeAllObjects];
[visibleHexes addEntriesFromDictionary:[map visibleHexesFrom:hex withinRange:SIGHT_RANGE]];
}

@end

This code runs perfectly. But the glitch is that when I build the project, the line:
[visibleHexes addEntriesFromDictionary:[map visibleHexesFrom:hex withinRange:SIGHT_RANGE]];
generates a warning:
'Map' may not respond to '-visibleHexesFrom:withinRange:'

Is there a way to eliminate the warning? When the program runs, Map responds to the method just fine. Am I not importing or forward referencing something that I should be? How does the Map class even know about the LineOfSight category?

Thanks,
Newbie Greg



robbieduncan
May 10, 2009, 12:25 PM
You need to import the header for the category as well into classes you call category methods from.

lee1210
May 10, 2009, 01:45 PM
You need to import the header for the category as well into classes you call category methods from.

I'm not that familiar with category stuff, but i would think importing the category header instead would suffice, since it should already be importing the class on which it is a category. Is this true?

-Lee

robbieduncan
May 10, 2009, 01:47 PM
I'm not that familiar with category stuff, but i would think importing the category header instead would suffice, since it should already be importing the class on which it is a category. Is this true?

-Lee

Yeah, you are correct.