Obj-C Category methods not found

Discussion in 'Mac Programming' started by GregX999, May 10, 2009.

  1. GregX999 macrumors newbie

    GregX999

    Joined:
    Apr 5, 2009
    #1
    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:

    Code:
    #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:

    Code:
    #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:
    Code:
    #import "Map.h"
    Create a pointer to the Map object that was created by yet another class, "GameData".

    Code:
    - (void)prepareForNewMap
    {
    	map = gameData.map;
    	...do some other stuff...
    }
    And finally, I have this method:

    Code:
    - (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:
    Code:
    [visibleHexes addEntriesFromDictionary:[map visibleHexesFrom:hex withinRange:SIGHT_RANGE]];
    generates a warning:
    Code:
    '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
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You need to import the header for the category as well into classes you call category methods from.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Yeah, you are correct.
     

Share This Page