简体   繁体   中英

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:

there was a declaration like that on the @interface section

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.

In that second piece of code, self.fetchMe grabs the fetchMe_ property using its setter. If this guy had forgotten to use self. and simply written

FetchClass *fetchOne = [fetchMe contrast];

He'd get an error, since fetchMe doesn't exist (but fetchMe_ does). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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