TigerCoding
TigerCoding

Reputation: 8720

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.

Upvotes: 1

Views: 444

Answers (2)

TigerCoding
TigerCoding

Reputation: 8720

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?

Upvotes: 0

bandejapaisa
bandejapaisa

Reputation: 26952

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

Upvotes: 2

Related Questions