ObjC: proper use of property and synthesize?

前端 未结 3 1557
迷失自我
迷失自我 2021-01-29 04:06

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 c

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-29 04:29

    This works on my system (macOS):

    #import 
    
    @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

提交回复
热议问题