0

Does anyone know why this code is running into compilation errors? I'm compiling it on Catalina using clang. I posted a similar issue here but that was when I was trying to compile using clang on Linux. I thought getA and setA are auto-generated by synthesize. 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;
}

Compilation:

$ clang -framework Foundation otest0.m -o hello
otest0.m:23:16: warning: instance method '-getA' not found (return type defaults
      to 'id') [-Wobjc-method-access]
    int v = [a getA];
               ^~~~
otest0.m:3:12: note: receiver is instance of class declared here
@interface A: NSObject
           ^
otest0.m:23:9: warning: incompatible pointer to integer conversion initializing
      'int' with an expression of type 'id' [-Wint-conversion]
    int v = [a getA];
        ^   ~~~~~~~~
2 warnings generated.
notaorb
  • 1,944
  • 1
  • 8
  • 18

3 Answers3

1

The getter/setter pair is synthesized as

-(int)a;
-(void)setA:(int)val;

So you need:

int main (int argc, char * argv[])
{
  @autoreleasepool {
    A *a = [[A alloc] init];

    [a setA:99];
    int v = [a a];
    NSLog (@" %d\n", v);
  }
  return 0;
} 
Warren Burton
  • 17,451
  • 3
  • 53
  • 73
0

Declaring a property with name a produces a getter with name a, not getA. This is what the first warning is about: "instance method '-getA' not found"

Russian
  • 1,296
  • 10
  • 15
-2

This works on my system (macOS):

#import <Foundation/Foundation.h>

@interface A: NSObject

@property int a;

@end

@implementation A {
  int a;
}
@synthesize a;

-(int) getMyValue {
return a;
}

@end

int main () {
  @autoreleasepool {
    A *a = [[A alloc] init];
   [a setA:99];
   NSLog (@"value = %d", [a getMyValue]);
 }
  return 0;
}

If file is saved as synth.m the terminal command is: clang synth.m -framework Foundation -o synth && ./synth

apodidae
  • 1,988
  • 2
  • 5
  • 9
  • 1
    I can confirm you code works. Now, shouldn't synthesize create two methods getA and setA? I try changing getMyValue to getA and that results in compilation errors. Do you observe this as well? Thanks – notaorb May 24 '20 at 20:25
  • If you change the method to -(int)getA and then use [a getA] in the main to retrieve it, it should compile without error. – apodidae May 24 '20 at 20:32
  • I think you're expecting there to be a "get" for every "set", when in fact objc does not use the word "get" for the getter, only "set" for the setter. – apodidae May 24 '20 at 20:55
  • 1
    every where I'm reading says that synthesize automatically creates a getter and/or setter method. So you're saying it only creates a setter? Thanks – notaorb May 24 '20 at 21:11
  • No, it creates both, but you can only use "set" for the setter. The creators of objc did not use "get" for the getter. According to Kochan in ''Programming in Objective-C": methods that set the values of instance variables are often collectively referred to as setters, and methods used to retrieve the values of instance variables are called getters. – apodidae May 24 '20 at 22:18