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 static keyword, so the variable is only available from the implementation file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With