对象:正确使用属性并进行合成?

编程入门 行业动态 更新时间:2024-10-21 03:46:10
本文介绍了对象:正确使用属性并进行合成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人知道为什么这段代码会遇到编译错误吗?我正在使用clang在Catalina上进行编译。我在此处发布了类似的问题,那是我尝试使用在Linux上发出叮当声。我以为getA和setA是由synthesize自动生成的。谢谢!

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; }

编译:

$ 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.

推荐答案

getter / setter对合成为

The getter/setter pair is synthesized as

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

所以您需要:

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; }

更多推荐

对象:正确使用属性并进行合成?

本文发布于:2023-11-07 06:12:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565751.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   正确

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!