Search code examples
objective-cpropertiesivar

Objective-C: Making iVars @public or @synthesizing @properties for them? When should I use properties?


I have some instance variables in my class that I'd want to be accessible anywhere. Like so:

@interface SomeObject : NSObject
{
    @public
        NSString *someString;
}
@end

@implementation SomeObject
@end

I can access the property from the instance using the -> syntax like below, as I would do in C++:

someObjectInstance->someString

Should I make a property for someString when all I want is for it to be accessible by the outside world? I would create a @property for someString in my interface and @synthesize it in my implementation, which would enable me to access it using the dot syntax.


Solution

  • Generally speaking, if you want to expose data, you should use properties. Making instance variables public is a bad idea in general.