什么是 Setter 和 Getter?
我对 Objective-C 的 Setter 和 Getters 并没有真正的了解。有人可以为初学者提供一个好的指南吗?我注意到,当尝试访问另一个类中的变量时,这会发挥作用,我现在正在尝试这样做。我有两个类,比如说 A 和 B。我在 A 中有一个 NSString 变量,带有 @property (retain) NSString * 变量。然后我继续合成它。现在,当视图在类中加载时,我将变量的值设置为“hello”。现在我想做的是从类 B 访问字符串。我已经导入了类 A,并用以下代码初始化它:
AClass *class = [[AClass alloc] init];
NSLog(@"Value:%@", class.variable);
[class release];
但是在调试器中它返回一个值“(null)”,我实际上并不知道理解。如果有人能引导我走上正确的道路,我将不胜感激。
谢谢,
凯文
I don't really have a solid understanding of Setters and Getters for objective-c. Can someone provide a good guide for beginners? I noticed that this comes into play when trying to access variables in another class, which I am trying to do right now. I have two classes, lets say A and B. I have a NSString variable in A with the @property (retain) NSString *variable. Then I go ahead and synthesize it. Now when the view loads in the class I set the value for the variable to "hello". Now what I want to do is access the string from class B. I have imported the the class A, and initialized it with this code:
AClass *class = [[AClass alloc] init];
NSLog(@"Value:%@", class.variable);
[class release];
However in the debugger it returns a value of "(null)", which I don't really understand. If someone could lead me into the right path I would greatly appreciate it.
Thanks,
Kevin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您感兴趣的特定部分是声明的属性< /a>.
b 的接口应如下所示:
您的 b 实现应如下所示:
然后您可以这样调用:
The particular section of interest to you is Declared Properties.
b's interface should look like:
Your implementation of b should look something like:
Which you could then call like:
iOS 参考中的iOS 入门指南图书馆概述了您应该阅读的阅读材料,以了解此类基础知识。 Apple 的指南写得清晰且详尽,如果您认真阅读它们,将会对自己大有裨益。
The Getting Started with iOS guide in the iOS Reference Library outlines the reading material you should go through to nail down basics like this. Apple's guides are clearly written and thorough, and you'll be doing yourself a huge favor by hunkering down and just reading them.