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!
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
statickeyword, so the variable is only available from the implementation file.