Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing protected instance variables in subclasses in Objective-C

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?

like image 362
user1899125 Avatar asked Oct 02 '13 20:10

user1899125


1 Answers

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
like image 66
Brian Nickel Avatar answered Oct 21 '22 12:10

Brian Nickel