tags:

views:

48

answers:

2

Hi there,

Could someone please explain the Objective-C difference between myString and anotherString in the following snippet:

   // In .h file
   @interface MyClass : NSObject {
 NSString* myString;
   }
   @end

   // In .m file
   @interface MyClass ()
   NSString* anotherString;
   @end

   @implementation MyClass
   //...
   @end

Thanks!

+2  A: 

In the .m file, the @interface MyClass() is actually a category, not a proper interface declaration. The difference is that categories can not add instance variables, only methods.

Tom Dalling
+3  A: 

In the .h file, you declare an instance variable. Each object will have a different one.

In the implementation file, you declare a global variable (the fact it's in a category does not change anything).
So the value of that variable will be the same, no matter the object's instance.

Note that this is often useful to simulate class variables, but with the static keyword, so the variable is only available from the implementation file.

Macmade