Question

I'm trying convert my code to Modern Objective-C style. How i read here http://www.techotopia.com/index.php/The_Basics_of_Modern_Objective-C": "In the case of Modern Objective-C, however, the synthesis takes place by default, making the use of @synthesize declarations unnecessary. When using default property synthesize, instance variable properties are accessible from within code using the property name prefixed with an underscore."

However, I have:

Relationship.h

@interface Relationship : NSObject <NSCoding>
//...
@property(nonatomic, weak) Person* first;
//...
@end`

OtherRelationship.h

#import "Relationship.h"

@interface OtherRelationship : Relationship

@end

OtherRelationship.m

#import "OtherRelationship.h"

@implementation OtherRelationship

@synthesize first = _first;

- (void)foo
{
NSLog(@"%@", _first);
}

and it's working. But when i delete

 @synthesize first = _first;

i get "Use of undeclared identifier '_first'" error. Does inheritanced variables doesn't work with autosynthesize or should i looking for problem elsewhere?

Was it helpful?

Solution

The backing ivar in the superclass is @private to the subclass. That is, the subclass may call self.first, but not _first. If you want to @synthesize again, use a different name because you can't refer to _first. For example, replace with @synthesize first = _ffirst; or just drop the @synthesize.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top