TL;DR What was the correct was to access a Objective-C @property (...) foo
as foo
instead of self.foo
?
Details:
One of my iOS project still uses some Objective-C code I have written several years ago.
// SomeObject.h
@interface SomeObject : NSObject
@property (nonatomic, readonly) NSString *foo;
@end
// SomeObject.m
@implementation SomeObject { ... }
@synthesize foo;
- (void)someMethod {
foo = @"bar";
}
- (void)otherMethod {
NSLog(@"Foo: %@", foo); // ---> "bar"
}
@end
This works fine as intended. Now I would like to use a getter to access some new Swift code instead of setting foo
manually:
// SomeObject.m
@implementation SomeObject { ... }
@synthesize foo;
- (NSString *)foo {
return [SwiftObject getFoo];
}
- (void)otherMethod {
NSLog(@"Foo: %@", foo); // ---> nil
NSLog(@"Foo: %@", self.foo); // ---> "swiftBar"
}
@end
So, using the getter only works correct when using self.foo
instead of just foo
. This is not a big problem but it would require to replaces all usage of foo
with self.foo
in the existing code.
Is there a clean way to avoid this, use the foo
getter and still use foo
instead of self.foo
?