Goal C: Different behavior variables in the header file (.h) compared to the implementation file (.m)

Can someone explain the difference between Objective-C between myStringand anotherStringin the following snippet:

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

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

   @implementation MyClass
   //...
   @end

Thank!

+3
source share
2 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 that it does not change anything in the category).
Thus, the value of this variable will be the same regardless of the instance of the object.

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

+4

.m, @interface MyClass() , . , , .

+2

Source: https://habr.com/ru/post/1754188/


All Articles