views:

65

answers:

1

Hi, I have a few properties defined in my header file like so

@property (assign) bool connectivity_N;
@property (assign) bool isConnected_N;

In my implementation file I have an init and the synthesized properties like so

@implementation Map
@synthesize connectivity_N;
@synthesize isConnected_N;

a init to set the initial values like so

-(id) init
{
    if( (self=[super init]) ) 
    {

        //initialise default properties
        self.connectivity_N=NO;
            self.isConnected_N=NO;
    }
    return self;
}

I'm running into an error that states Error: accessing unknown 'connectivity_N' class method. In this public method within the class

+(bool) isConnectable:(directions) theDirection{

    bool isTheDirectionConnectable= NO;
    switch (theDirection) {
        case north:
            isTheDirectionConnectable= self.connectivity_N;
            break;

I'm not sure why is this so as I'm trying to grab the value of the property. According to the apple developer documentation "The default names for the getter and setter methods associated with a property are propertyName and setPropertyName: respectively—for example, given a property “foo”, the accessors would be foo and setFoo:"

That has given me a clue that I've done something wrong here, I'm fairly new to objective C so would appreciate anyone who spends the time to explain this to me.

Thanks!

+3  A: 

You've defined isConnectable: as a class method by prefixing it with +. Probably you want it to be an instance method -- start it with a minus sign - instead.

You can't access self within class methods, because there is no object instance.

Although self exists in class methods, it doesn't refer to an object instance -- there is no object -- so you can't access object properties. (Thanks to Dave DeLong for the correction.)

walkytalky
+1, except that `self` does work within class methods. It refers to the `Class` itself. (so you can do things like `[self myClassMethod]`)
Dave DeLong
Ah, thanks, that's interesting. It's never even occurred to me to try that!
walkytalky
It's actually better to do it that way so inheritance works normally. For example, if your `+[Foo foo]` class method calls `[[[self alloc] init] autorelease]` rather than `[[[Foo alloc] init] autorelease]`, then subclasses get it for free.
Chuck