8

Why if I declare a variable in an @implementation block between the {} brackets, does attempting to access the variable in a subclass yield a compile error?

1
  • 1
    Are you declaring the ivar in the @interface or @implementation? Commented Oct 2, 2013 at 20:48

2 Answers 2

4

It depends on where you're placing your instance variables. Modern Objective-C lets you place them in your @interface or @implementation, or not declare them at all with @synthesize and auto-synthesize.

Imagine we have a class A:

A.h

@interface A : NSObject
{
@protected
    int i;
}
@end

A.m

#import "A.h"

@implementation A
{
@protected
    int j;
}
@end

When we declare a subclass B, we import the header and can see the declaration of i, but since we cannot import the implementation, we cannot know about the declaration of j.

The following code produces one error, on the j line.

#import "A.h"

@interface B : A
@end

@implementation B
- (int)i {return i;}
- (int)j {return j;}
@end

Update/Additional note

In addition to implementing classes in their own files (C.m) you can declare multiple implementations in a single file. In this case, these classes can access @implementation ivars declared in the superclass:

C.h

#import "A.h"

@interface C : A
@end

A.m

#import "A.h"
#import "C.h"

@implementation A
{
@protected
    int j;
}
@end

@implementation C
- (int)j {return j;}
@end
Sign up to request clarification or add additional context in comments.

2 Comments

ok that's exactly what feedback, but then why give the possibility to declare variables public or protected in the @implementation if not then I can use them?
If you declare multiple class implementations in a single file, those modifiers become meaningful.
1

Instance variables declared in a class's @implementation section are private by default, and therefore not visible in subclasses. You can use the @protected visibility modifier to change the visibility.

@implementation MyClass
{
    int _foo;  // Can't be accessed directly in subclass code.

@protected     
    int _bar;  // Can be accessed directly in subclass code.
    int _baz;  // Can be accessed directly in subclass code.
}

1 Comment

unfortunately I get an error from xcode when I try to access "_bar"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.