3

Whats the difference between these two:

@interface MyClass ()
{
    BOOL newUser;
}

Or

@implementation MyClass
{
BOOL newUser;
}

@end
kamran619
  • 531
  • 10
  • 32
  • 1
    In the second case, even the existence of the instance variable won't be known to the user of the class, only to the class itself. In the first case, however, whoever imports the header with the class declaration will know that there are some instance variables named this and that, even if they're declared as `@private`, `@protected` or `@package`. –  Jul 30 '13 at 20:23
  • @H2CO3 The first one is a class extension (most likely in a .m, not a .h). – rmaddy Jul 30 '13 at 20:26
  • @rmaddy Exactly (however, terminology wasn't my main point). –  Jul 30 '13 at 20:26
  • 4
    @H2CO3 But the point is, since the 1st one is a class extension in a .m file, no one will be importing it and the ivar is private. – rmaddy Jul 30 '13 at 20:27
  • @rmaddy "In the second case, even the existence of the instance variable won't be known to the user of the class, only to the class itself." –  Jul 30 '13 at 20:28
  • 1
    @H2CO3 But I've been talking about the 1st case, not the 2nd. – rmaddy Jul 30 '13 at 20:29
  • @rmaddy Ah, yes, whatever. Disregard what I wrote. –  Jul 30 '13 at 20:30

1 Answers1

1

variables declared in your interface, as in 1., are visible in other classes that instantiate objects of MyClass. The variable declared in 2. will only be visible inside of MyClass. Here is something you should read: http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/

EDIT: @JoshCaswell is right. 1. is an anonymous category. Its varaibles will be seen depending on where the interface is declared. a better link to read about this is: http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html

katzenhut
  • 1,742
  • 18
  • 26
  • 1
    The first declaration is in a class extension, which may not be (usually is not) in a public header. Moreover, variables declared in extensions are private by default, not `@protected`. – jscs Jul 30 '13 at 20:28