简体   繁体   English

iPhone-有关@property的问题

[英]iPhone - a question about @property

I am kind of newbie on Objective-C and I was looking at a code, trying to understand a few things, and I come across with this .h file: 我是Objective-C的新手,我在看一段代码,试图理解一些东西,然后遇到了这个.h文件:

there was a declaration like that on the @interface section @interface部分上有这样的声明

MyVideoClass *contrast_;

then below we have 然后在下面

@property (nonatomic, retain) MyVideoClass *contrast;
@property (nonatomic, retain) FetchClass *fetchMe;

The strange part is that the first has an underscrore after the name and the second one, doesn't. 奇怪的是,第一个在名称后有一个下划线,而第二个则没有。

The other strange thing is that the guy has a call to these properties like this: 另一个奇怪的事情是,该人调用了这些属性,如下所示:

FetchClass *fetchOne = [self.fetchMe contrast];

What kind of call is that? 那叫什么电话 This seems pretty insane to me. 对我来说这似乎很疯狂。 I simply cannot understand what is going on here, but the code works. 我根本不明白这里发生了什么,但是代码有效。 pretty insane. 太疯狂了。

Can you guys explain me that? 你们能给我解释一下吗? Forgive the stupid question, but I am still learning... 原谅这个愚蠢的问题,但我仍在学习...

thanks 谢谢

Check the top of the implementation file; 检查执行文件的顶部; you should see a line that reads 您应该看到一行显示

@synthesize contrast = contrast_;

The reason one would do this would be to make sure that you access properties through their setters and getters (created with @synthesize ), rather than directly. 这样做的原因是要确保您通过属性的设置器和获取器(使用@synthesize创建)而不是直接访问属性。

In that second piece of code, self.fetchMe grabs the fetchMe_ property using its setter. 在第二段代码中, self.fetchMe使用其设置程序fetchMe_属性。 If this guy had forgotten to use self. 如果这个家伙忘了使用self. and simply written 并简单地写

FetchClass *fetchOne = [fetchMe contrast];

He'd get an error, since fetchMe doesn't exist (but fetchMe_ does). 他会得到一个错误,因为fetchMe不存在(但fetchMe_确实存在)。 As with all things, it's up to you whether or not to use protection. 与所有事物一样,是否使用保护取决于您。

This: 这个:

FetchClass *fetchOne = [self.fetchMe contrast];

Is exactly equivalent to any of these: 完全等同于以下任何一个:

FetchClass *fetchOne = self.fetchMe.contrast;
FetchClass *fetchOne = [[self fetchMe] contrast];

That is, the . 也就是说, . is equivalent to a method call. 等效于方法调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM