1

I want to declare static-class variables in objective-c class and want to use them directly using class name like

E.g

having a class named "Myclassname" and variable "Var"

and want access this variable like this....

Myclassname.Var=@"hi";

I dont want to use getters and setters.

Any help please?

vishnu
  • 113
  • 5
  • 3
    Not possible. [Take a look at this question](http://stackoverflow.com/questions/695980/how-do-i-declare-class-level-properties-in-objective-c). – bbarnhart Feb 13 '13 at 14:17
  • For accessing static ivars from external you should use accessors. Keep in mind that the accessors start with a prefix + and not - because they are class methods rather than instance methods. If you var is `int _abc;` and your getter is `+(int)abc;` then and your Class is `A` then you access the getter `with `[A abc]; I am not sure whether `A.abc` would work too. Don't think so. – Hermann Klecker Feb 13 '13 at 14:21

2 Answers2

1

Variables aren't accessed using the . syntax - those are getters and setters. Your only option, as @bbarnhart points out, is to manually declare class getters and setters.

@interface Myclassname

+(NSString *)var;
+(void)setVar:(NSString *)newVar;

@end

And implement these methods to access/set the backing static variable.

This isn't really a good idea, anyway, and doesn't jive with Objective-C style. You should consider using a singleton and properties, instead.

Community
  • 1
  • 1
Ash Furrow
  • 12,391
  • 3
  • 57
  • 92
0

I don't see why you can't use the -> syntax like this:

Myclassname->var = @"Hi";
JiuJitsuCoder
  • 1,866
  • 14
  • 13