什么是 Setter 和 Getter?

发布于 2024-09-28 16:36:30 字数 464 浏览 7 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情愿 2024-10-05 16:36:30

您感兴趣的特定部分是声明的属性< /a>.

b 的接口应如下所示:

@interface b : NSObject {
    NSString *value;
}

@property (nonatomic, retain) NSString *value;

- (id) initWithValue:(NSString *)newValue;

@end

您的 b 实现应如下所示:

@implementation b

@synthesize value;

- (id) initWithValue:(NSString *)newValue {
    if (self != [super init])
        return nil;

    self.value = newValue;

    return self;
}

@end

然后您可以这样调用:

b *test = [[b alloc] initWithValue:@"Test!"];
NSLog(@"%@", test.value);

The particular section of interest to you is Declared Properties.

b's interface should look like:

@interface b : NSObject {
    NSString *value;
}

@property (nonatomic, retain) NSString *value;

- (id) initWithValue:(NSString *)newValue;

@end

Your implementation of b should look something like:

@implementation b

@synthesize value;

- (id) initWithValue:(NSString *)newValue {
    if (self != [super init])
        return nil;

    self.value = newValue;

    return self;
}

@end

Which you could then call like:

b *test = [[b alloc] initWithValue:@"Test!"];
NSLog(@"%@", test.value);
何必那么矫情 2024-10-05 16:36:30

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文