How to use static variable(BOOL) in Objective C - iphone

I am from C# background, and I am having a hard time in figuring out about how to use a static variable(BOOL in my case) in Objective C.
My questions are:
Where should I declare my static variable.
How can I access(set its value) from another class.
Do I need to use extern keyword.

Declare static variable in your implementation file and provide class method to set/get vlaue of it.
// MyClass.h
#interface MyClass : NSObject {
}
+ (BOOL)gBoolean;
+ (void)setGBoolean:(BOOL)value;
#end
// MyClass.m
#import "MyClass.h"
static BOOL gBoolean;
#implementation MyClass
+ (BOOL)gBoolean; {
return gBoolean;
}
+ (void)setGBoolean:(BOOL)value; {
gBoolean = value;
}
#end
Take a look at this answer.

Related

ios - How to assign a new class in existing class?

I'm new to iPhone development,I want to assign one class in existing class
This is what declare in C#
public partial class UserRegisteration : System.Web.UI.Page
{
//some methods
public class Mime
{
//some methods
}
}
like the above format in Objective-C how to assign one more class in existing class?
Any ideas? Thanks in advance.
Yes you can.I am using one class in existing class . One is extending UIView and other is extending NSObject.
MultiLayout.h
#interface MultiLayout : UIView
{
// methods and properties
}
#end
#interface SingleControl : NSObject
{
// methods and properties
}
#end
MultiLayout.m
#implementation SingleControl
//methods and variables declaration and property synthesization
#end
#implementation MultiLayout
//methods and variables declaration and property synthesization
#end
For getting the static value of SingleControl in MultiLayout . you have to call class method like:
MultiLayout.h
#interface MultiLayout : UIView
{
// methods and properties
}
#end
#interface SingleControl : NSObject
{
// methods and properties
}
// add class method
+(int)getValue;
#end
MultiLayout.m
#implementation SingleControl
// add static value
static int values = 100;
// implement method
+(int)getValue{
return values;
}
#end
#implementation MultiLayout
// call method to get value
[SingleChoiceControl getValue];
#end
If I understand your question correctly, you would do this in your interface file:
#interface Mime
//properties and method declarations
#end
#interface UserRegistration : System.Web.UI.Page
#property (strong, nonatomic) Mime *mimeInstance;
#end
and then in the implementation file you implement both like so:
#implementation Mime
//implementation
#end
#implementation UserRegistration
//implementation
#end

Incorrect use of static variables?

Class A has a UIImage.
Class B has a static reference to a class of type A.
Before class B is instantiated, I want to call a static method in class B to assign an instance of class A.
+ (void)setClassAReference:(ClassA*)classA
{
classA_ = classA;
}
Is this possible?
Before I delved into my current project, I created a sample one, and was able to set an integer value, then instantiate B with it keeping the stored value and allowing access to it.
However, in my current project, XCode refuses to allow me to pass an integer value:
Non-static method in class A:
- (UIImage*)imageWithIdentifier:(ImageIdentifier)identifier; // identifier is enum type
After class B is instantiated, I try to call a method in A:
UIImage *img = [classA_ imageWithIdentifier:ImageIdentifier_Foo];
But I get an implicit conversion warning. The auto-complete shows (id) instead of (ImageIdentifier). I've triple-checked all my method signatures and they all use the enum type.
Am I using static variables incorrectly or is there another problem? I realize I could use a singleton, but I'd prefer not to if possible.
I'm adding the enum declaration here:*
typedef enum
{
ImageIdentifier_Foo = 0,
ImageIdentifier_Bar
} ImageIdentifier;
*real names changed to protect the innocent.
Firstly...
If you want to initialize static variables on a class before it is instantiated you use the class method on NSObject
+ (void) initialize
This is where you can assign your static ClassA variable in ClassB.
Secondly....
Make sure you retain that classA variable, otherwise it will be released.
Thirdly.....
Regarding your implicit conversion... what is variable 'a', above this you wrote classA_. Can you show your enum declaration. Have you imported ClassA ?
I don't have any compile error with this:
ClassA.h
typedef enum
{
ImageIdentifier_Foo = 0,
ImageIdentifier_Bar
} ImageIdentifier;
#interface ClassA : NSObject
- (UIImage*)imageWithIdentifier:(ImageIdentifier)identifier; // identifier is enum type
#end
ClassA.m
#import "ClassA.h"
#implementation ClassA
- (UIImage*)imageWithIdentifier:(ImageIdentifier)identifier {
return nil;
}
#end
ClassB.h
#interface ClassB : NSObject
#end
ClassB.m
#import "ClassB.h"
#import "ClassA.h"
static ClassA *classA;
#implementation ClassB
+ (void) initialize {
classA = [[ClassA alloc] init];
}
- (void) doSomething {
UIImage *image = [classA imageWithIdentifier:ImageIdentifier_Foo];
NSLog(#"image %#", image);
}
#end
The error got cleared up.
I was importing Class A in the .h file of Class B. It was also being imported in the .m file of class B. I removed the import in the .h file, and changed it to #class ClassA and everything automagically resolved itself.
Would a circular reference have caused this?

How do you differentiate private member variable in objective-c?

for private methods,
I could use
#interface MyClass(PrivateMethods)
- (void) _foo;
#end
How do you declare a private member variable in objective c?
http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocDefiningClasses.html#//apple_ref/doc/uid/TP30001163-CH12-TPXREF127
#interface MyClass : Superclass {
#private
NSString * privateString;
}
#end
You can declare it as follows..
#private //variable;
Check this question out.
Question- private-instance-variable

Release static int variable

How to "clean" static int variables in a view class method? Every time a get back to this view I need those vars "zeroed". The [self.view removeFromSuperview];
instruction does not seem enough to free up memory from those vars.
Thank you. Have a great 2010!
These int vars are declared static in a view method. They are not global in the view class.
If you don't want a static value to stick around, don't make it static.
You'll have to manually do this by defining a setValue method similar to:
#interface MyClass
{
// ...
}
+ (NSString *)myVar;
+ (void)setMyVar:(NSString *)newVa;
#end
#implementation MyClass
static NSString *myVar;
+ (NSString *)myVar { return myVar; }
+ (void)setMyVar:(NSString *)newVar { myVar = newVar; }
#end

Static classes with iPhone

I was just wondering if this is possible... if I have a "Static class" (a class with a bunch of static methods) is it possible to have a class variable and access it through one of the static methods?
I am getting a warning of "instance variable accessed in class method".
I maybe just not getting it. Is there anyone that can answer this question?
You can use static variables to implement the equivalent of class variables:
// Foo.h
#interface Foo : NSObject {
}
+ (NSObject*)classVariable;
#end
// Foo.m
#import "Foo.h"
static NSObject* classVariable;
#implementation Foo
+ (NSObject*)classVariable {
return classVariable;
}
#end