How to use class variables in objective-c like "MyClassName.var"? - ios

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?

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.

I don't see why you can't use the -> syntax like this:
Myclassname->var = #"Hi";

Related

instance variables only visible internally in class

NOTE: I am only talking about .m file here.
I am confused by these two things, both are said to be the way to declare internal instance variable:
Way 1:
#interface MyClass ()
// Declare instance variable which is only visible in this class
#end
#implementation MyClass
...
#end
Way 2:
#implementation MyClass {
// Declare instance variable which is only visible in this class
}
...
#end
What are the differences between these two ways of declaring instance variables?
In terms of functionality, there is no difference.
Declaring the ivars in a class extension -- the #interface MyClass() {...ivars...}#end pattern -- does allow you to move the ivar declaration to a header file that could then be used by other classes for direct access for internal-only use, for example.
After the "#interface" line, you can add instance variables within curly braces. That's instance variables, not properties. After the curly braces, or immediately after the "#interface" line if you have no curly braces, you add methods and properties.
So your first comment is wrong. Instance variables can only be added within the curly braces. Properties generate instance variables (usually but not always), but they are not instance variables.
Besides ivar, properties also generate setter and getter method if necessary (according your property attribute). But if you implement the setter and getter synchronously, compiler would not synthesize ivar, since the compiler will assume that you are taking control over the property implementation and won’t synthesize an instance variable automatically.
In Objective C the { } it’s the area where you declare instance variables. You can also use access member declaration in that section like #private #public & #protected. When you declares the "ivars" outside the {} the variable will be a file-scope variable (kind of static) and it won't be an instance variable of your class.
If you use properties (#property) the compiler is smart enough and will create its own ivars with its corresponding setter and getter.
Using a class extension in the implementation file allows you to add ivars or properties (and methods) that are only accessible by the implementation of the class extension. It also allows you to overwrite access of base classes like properties or private methods.

Expose a protected Objective-C instance variable to subclass

I am subclassing a pod's class, and in this class there's a private instance variable that I want to expose and use within my class:
#interface MySuperClass () {
UIScrollView *_scrollView;
}
Usually with exposing a private member or method, I would use a category like someone previously mentioned here, but I am having a problem doing it with a private instance variable. I read here that Associative References might work, but I wasn't able to make it work.
Try implementing in child class:
- (UIScrollView *)scrollView {
return [self valueForKey:#"_scrollView"]
}
Unfortunately, in Objective-C there is no way to declare private instance variables.
Whatever you want your subclass to be able to see, you'll have to declare in your .h-file. The Associative References that you were talking about work in that exact same way, but they solve a different problem, namely the one of declaring instance variables in a category.
This is due to the design of the language, and I guess it makes sense in the way that .m files are really implementation files, and no other class should actually care about the implementation of another, even with inheritance relationships like subclassing.
The option for you with the private instance variable of that pod's class would be to either put it in a property or indeed implement a category where you add methods to access it.

When would you declare variables in package instead of as properties?

I have noticed in many of the header documentation files in the Apple framework that define variables within brackets directly following the interface definitions like the variables a and b below:
#interface MyView : UIView {
#package
int a;
UIView b;
}
Normally I have just been declaring all variables as properties for the convenience of the synthesized methods. Can anyone tell me when it would be more efficient or more proper to declare variables under #package instead of as #properties?
#package is an access specifier, similar to how it works in Java (it allows access from any code at the same package level). But since the . syntax with Objective C objects works with properties, and not direct member access, you have to use the -> syntax instead, as if the object were a C struct, to access that variable directly.
An auto-synthesized #property instead creates a protected instance variable (prefixed with an underscore), which is read and set by the synthesized methods. Now, the overhead of using a property is negligible, compared with direct member access, so there's no real reason to stop using properties.
In your example, if you had a MyView* myview, you could set the view b directly, with myview->b = someotherview;. But this would give the class no chance to respond to the update (you would probably write the setter method so you can do something with it), nor does it ensure that the view provided is retained properly. These are the issues that properties were designed to avoid.

Objective C - Add property in runtime

I'd like to add an ivar to an existing objective-c class in runtime, but documentation states that an ivar cannot be an existing class, so I think property could still solve my issue.
As stated here class_addProperty(...) returns true, but when I try to access the ivar by it's name (or the property name) it always returns nil. What could be the issue causing this to happen?
You won't be able to add an ivar to the class at runtime. You can think of the class, and its ivars, as something like a C struct. It's layout is defined at compile time.
You can add properties at runtime (since these are just methods), and you can implement their getters and setters, but you'll need to come up with a different way to store any data that they represent.
Are you looking for something similar with some other programming language?
it looks like adding properties in AS3, but objc think the best would you use to store NSDictionary objects by keys.

Newbie: about ios / objective -c variable definition in a class

I am new in objective-c and iOS development. I have a simple question to ask:
I saw in some iOS class implementation file, people use the code like below:
#implementation BIDMyController
- (void)viewDidLoad{...}
The code above is quite straight forward. But, sometimes, I saw code of class implementation like below:
#implementation BIDMyController{
NSMutableArray *names;
}
- (void)viewDidLoad{...}
The only difference is that there are curly brackets added, which includes some variable definition. What does the curly brackets & the variables mean? Are they the Object-C style of defining private variables of this class ?? Could someone explain to me?
Yes, it's a way of declaring instance variables (ivars) that are only visible internally.
See: https://stackoverflow.com/a/6891326/1597531
First let's explain what are iVars, and then why they're declared on the implementation file:
Those are called instance variables (iVars) they are not specifically private variables.
You can change the way iVars behave with the following directives:
#private
The iVar will be accesible only by the class which declared it.
#protected
The iVar will be accesible by the class which declared it and by any subclasses. If you don't declare a directive this is used by default, and explains why you may think this is a way of declaring private variables.
#public
The iVar will be accesible from anwyehre.
#package
Accesible anywhere the app or static lib.
If you feel confused about some terms, don't worry. Most of the time there's no need to write the directive since #protected is the default one and it just work fine.
So, a iVars declaration will look like this:
#interface BIDMyController{
#protected
NSString *protectedString
#public
NSString *publicString
NSString *piblicString2 //This iVar is public since it's after de #public directive
}
I'm declaring variables on the interface file, although as you pointed out, they can be declared on the implementation file. The only difference is that declaring the iVars on the implementation file is a way of hiding private iVars. More about this here

Resources