1

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.

2
  • Why does the class B need to have a reference to an instance of A before an instance of B is created? Every instance of B will then have the same instance of A. Commented Feb 11, 2012 at 20:15
  • 'A' holds a few different images used by several sub-classes of 'B.' I want to make the classes re-usable, so each app might have different image names. I want to avoid having each sub-class send the same image name each time one is created by only sending it once in a static method. Commented Feb 11, 2012 at 20:19

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Class A has it's own init method, which calls [super init] like any other class. I'm not retaining, because I use ARC. I made a mistake in my code, I changed the variable name for 'a'. Yes, the class with type names are is imported. Strangely, in class A, I now see errors in the interface method signatures 'Expecting a type' but header for the type is imported and the type name is in blue, clearly showing the compiler recognizes the type name. Add the enum declaration above. What I'm doing should be possible correct? Maybe XCode is being weird?
Thank you for your help. This was driving me crazy.
0

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?

2 Comments

Dont think so. I just tried that with my sample code and still no problems.
I think Xcode just got screwed up then (wouldn't be the first time). Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.