Nirma
Nirma

Reputation: 5790

Do properties implicitly create a class member of that name and type?

I was looking through a Storyboard tutorial here: http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

When I noticed that the author created properties with a name and type without creating the member variables in the interface section. It was to my understanding previously that the @property/@synthesize declaration in objective C only created a getter / setter for a member variable of that name and type. I didn't think that the variable was implicitly created with the @property/@synthesize. Is the class member implicitly created? If not how does this code work:

@interface Player : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *game;
@property (nonatomic, assign) int rating;

@end

Upvotes: 2

Views: 212

Answers (1)

Matt Wilding
Matt Wilding

Reputation: 20153

Yes, it's implicitly created with the @sythesize keyword if an ivar of the specified name doesn't already exist. For example, given the above properies:

@synthesize name; // Creates an instance variable called "name"

Alternatively...

@synthesize name = _name; // Creates an instance variable called "_name"

Which means you no longer need to specify ivars in your class' @interface.

Upvotes: 3

Related Questions