Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C: Different behavior variables in header (.h) file vs. implementation (.m) file

Tags:

objective-c

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!

like image 532
Reuven Avatar asked Dec 05 '25 07:12

Reuven


1 Answers

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.

like image 198
Macmade Avatar answered Dec 07 '25 22:12

Macmade



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!