I've been coding in a vacuum for several years and am trying to get up to speed on best practices, in anticipation of my eventual return to employment. I'm hoping this is the best forum in which to ask these types of questions. If not, please let me know.

My first question regards assigning the initial values of a UIViewController's properties. In general, is it better to do this in viewDidLoad or in the property's getter?

For example, I have a UIViewController with graphics that are based on the user's chosen graphics Theme. The first thing that needs to happen is to load an NSDictionary for the chosen theme, based on the current setting in NSUserDefaults.

So to start, I have:

#import "MainViewController.h"

@interface MainViewController ()
@property NSString *currentTheme;

@implementation MainViewController
@synthesize currentTheme = _currentTheme;

My preferred method is to use lazy instantiation:

- (void)setCurrentTheme:(NSString *)currentTheme {
    _currentTheme = currentTheme;

- (NSString *)currentTheme {
    if (!_currentTheme) {
        _currentTheme = [[NSUserDefaults standardUserDefaults] objectForKey:@"currenttheme"];
    return _currentTheme;

But since this is the first thing that needs to happen, anyway, I don't think there's any real difference in performance or utility from assigning this in viewDidLoad:

- (void)viewDidLoad
    [super viewDidLoad];
    self.currentTheme = [[NSUserDefaults standardUserDefaults] objectForKey:@"currenttheme"];

Is there a preferred way of doing this?


1 Answer 1


I would recommend to use one common method for initialization of all the variables, constants, NSUserDefaults, etc..

viewDidLoad should not be messy, it should mostly have calls to the methods. I believe you are all aware with code reusability and code optimization techniques.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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