1
#import "ViewController.h"

@interface ViewController ()
{
    NSString *string;
}
@end

@implementation ViewController{
    NSString *string;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

What are the differences between these two statements? Which is better?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Vincent Sit
  • 2,214
  • 1
  • 24
  • 27
  • 1
    @BorisE That is a completely different question. – rmaddy May 02 '13 at 17:29
  • Note that the convention for ivars (and the way that implicit ivars are generated for properties) is to name them with an initial underscore: `_string`. Not required, but helpful to distinguish ivar accesses from property invocations. – Seamus Campbell May 02 '13 at 17:34

2 Answers2

6

Assuming the statements you are referring to are the two different declarations of the private instance variable string, then there is no difference. They are two different ways of declaring a private ivar. It's a matter of preference. Just pick one.

I personally use the one in the @implementation block. I only use the class extension for private properties and for declaring conformance to protocols.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

Well, latter is just the way of declaring variables without having to switch to .h file. That's just a matter of ease I'd might think.

atastrophic
  • 3,153
  • 3
  • 31
  • 50
  • It's more than a matter of ease. There is no reason to put private declarations in the .h file. Private implementation details should be in the .m file. This one of the major improvements in modern Objective-C. We no longer need to pollute .h files with private ivars, private properties, and private use of protocols. – rmaddy May 02 '13 at 17:39