Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Just started diving into Objective-C and IOS development and was wondering when and the correct location I should be declaring variables/properties. The main piece of code i need explaining is below:

Why and when should i be declaring variables inside the interface statement and why do they have the same variable with _ and then the same one as a property. And then in the implementation they do @synthesize tableView = _tableView (I understand what synthesize does)

Thanks :-)

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 
{
    UITableView *_tableView;
    UIActivityIndicatorView *_activityIndicatorView;
    NSArray *_movies;
}

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicatorView;
@property (nonatomic, retain) NSArray *movies;
share|improve this question
    
Also when is it appropriate to put ivars in the implementation file here @implementation ChecklistsViewController { ChecklistItem *row0item; ChecklistItem *row1item; ChecklistItem *row2item; ChecklistItem *row3item; ChecklistItem *row4item; } –  William Smith Jul 5 '12 at 4:52
add comment

2 Answers

First of all, there is no need to declare the instance variables for properties as the statement

@synthesize var = _iVar;

is actually generating an implicit variable named _iVar for you.

You would create properties when you want them to be accessible outside the class like

YourClass *obj = [[YourClass alloc] init];
obj.yourProperty = somevalue;

Properties also relieve you of writing your own getters & setters and take care of much of memory management themselves. Here is something you might wanna read plus other articles linked within the post.

http://cocoawithlove.com/2008/08/in-defense-of-objective-c-20-properties.html

share|improve this answer
add comment

Why and when should i be declaring variables inside the interface statement

The instance variables are optionally defined inside the interface statement. You can alternatively define them in the implementation (or, for synthesised properties, not at all).

why do they have the same variable with _

Having an underscore is just a convention for "private" variables. The convention is followed in other languages as well. These are not mandatory.

and then the same one as a property. And then in the implementation they do @synthesize tableView = _tableView (I understand what synthesize does)

Properties are a means of providing external objects access to variables of this class. If you intend to use a variable inside the class and do not mean/need to expose it to outside classes, then you do not need to define a property for it.

If your variable name and property name were the same you only have to write @synthesize variableName. However, if they are different e.g., when _ is added to the name by convention, then the statement @synthesize variableName = _variableName is telling that the property variableName sets and get the variable _variableName.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.