I'm trying to use @property and @sythesize correctly, but run into these errors. Does anyone know what's wrong with this code? This looks like how it should be used, but is not compiling correctly.
Thanks!
#import <Foundation/Foundation.h>
@interface A: NSObject
@property int a;
@end
@implementation A
int a;
@synthesize a;
@end
int main (int argc, char * argv[])
{
@autoreleasepool {
A *a = [[A alloc] init];
[a setA:99];
int v = [a getA];
NSLog (@" %d\n", v);
}
return 0;
}
clang-7 -o a.out otest1.m -I `gnustep-config --variable=GNUSTEP_SYSTEM_HEADERS` -L `gnustep-config --variable=GNUSTEP_SYSTEM_LIBRARIES` -lgnustep-base -fconstant-string-class=NSConstantString -D_NATIVE_OBJC_EXCEPTIONS -lobjc && ./a.out
otest1.m:12:13: error: synthesized property 'a' must either be named the same as a
compatible instance variable or must explicitly name an instance variable
@synthesize a;
^
otest1.m:22:16: warning: instance method '-getA' not found (return type defaults to 'id')
[-Wobjc-method-access]
int v = [a getA];
^~~~
otest1.m:3:12: note: receiver is instance of class declared here
@interface A: NSObject
^
otest1.m:22:9: warning: incompatible pointer to integer conversion initializing 'int' with
an expression of type 'id' [-Wint-conversion]
int v = [a getA];
^ ~~~~~~~~
2 warnings and 1 error generated.
411 [debian:~/src/c]$
UPDATE: The solution is that @synthesize creates method setA and a. Not setA and getA. I'm confused by this but it appears to be the problem in compilation. I tested the solution here using clang on Mac OS Catalina and clang on Linux and it works.