Incorrect use of static variables? - iphone

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?

Related

Undeclared variable from base class when derived class has property

I have the following code below, where a base class has a member which is (should be) accessible by a derived class.
The code below however gives a compilation error
...abcAppDelegate.m:30: error: 'baseVal_' undeclared (first use in this function)
If I call the variable using self->baseVal_ or if I remove the property defined in the derived class then everything is ok.
Also, if I define a category of the derived class, then I can access baseVal_ without error.
//---------------------------------------------------------------
// BASE CLASS
//---------------------------------------------------------------
#interface BaseClass : NSObject
{
#protected
BOOL baseVal_;
}
#end
#implementation BaseClass
#end
//---------------------------------------------------------------
// DERIVED CLASS
//---------------------------------------------------------------
#interface DerivedClass : BaseClass {
}
#property (readwrite) BOOL val;
#end
#implementation DerivedClass
#synthesize val;
- (void) foo {
baseVal_ = YES;
}
#end
Have a look here: Click. Seems to possibly be a bug with GCC, but it's easily fixable by adding val as an instance variable instead of using the property without.

How do I work between classes in Objective-C?

At the moment, the majority of my code is in the same viewcontroller, and i'd like to move some of it over to other areas. Such as moving the animations all over to somewhere else. But then how do i reference things which are in another class? And how do i reference back from that class to items in my viewcontroller class? Not going this has always disuaded me from doing it.
there is a couple of ways you can achieve that.
one way is the cocoa delegate #protocol way, the second way could be creating references to each object in the other class.
for the first way you can do something like this:
#class Class2;
#interface Class1 : NSObject {
Class2 *cls2Pointer;
}
#property Class2 *cls2Pointer;
#end
#class Class1;
#interface Class2 : NSObject {
Class1 *cls1Pointer;
}
#property Class1 *cls1Pointer;
#end
int main(){
Class1 cls1Obj = [[Class1 alloc] init];
Class2 cls2Obj = [[Class2 alloc] init];
[cls1Obj setCls2Pointer:cls2Obj];
[cls2Obj setCls1Pointer:cls1Obj];
}
the second way, is to declare a protocol in one/both of the classes to be able to pass arguments and call different methods on other objects:
#protocol Class1Delegate
- (void)class1:(Class1)obj MethodWithArg:(id)arg;
#end
#interface Class1 : NSObject {
id <Class1Delegate> delegate;
}
#end
#interface Class2 : NSObject <Class1Delegate>{
}
#end
#implementation Class2
- (void)class1:(Class1)obj MethodWithArg:(id)arg {
//do stuff when called from the 1st class
}
#end
You might like to look into this here - to create static classes in objective c and then reference them in a separate file by classname - as in the view controller quoted in the linked example.
Otherwise you can just create a new class within a separate .m file and then code it such that the calling method in another class will first create an instance of this new class and then invoke the necessary method on this instance.
Hope this helps.
Basically what you do is that you create one or more classes, move the code over to these classes and then create instances of these classes in your viewcontroller.
so if you had a method in your view controller
-(void)foo;
you would create a new class say C and move the method there.
then in your view controller you would create an instance variable of that class e.g.
C* myC;
then alloc/init and then call the foo method. This is not object oriented in the sense that foo is not really related to C in any way so method foo could have just been a static method not relating to the instance and as such called just like any other method but as [C foo] instead of [self foo] from the view controller.
the other more OOP method would be to move functionality that belongs to together into a separate class like animation in your example.

warnings in iphone sdk

i'm getting this two messages:
warning: 'MainView' may not respond to '-switchToNoGridView'
Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments
1st here:
//GridView.m
#import "GridView.h"
#import "MainView.h"
#implementation GridView
-(IBAction)switchToNoGridView {
[mainView switchToNoGridView];
}
#end
2nd here:
warning: 'MainView' may not respond to '-goBack'
Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments
in this:
//NoGridView.m
#import "NoGridView.h"
#import "MainView.h"
#implementation NoGridView
-(IBAction)goBack {
[mainView goBack];
}
#end
how to avoid these warnings?
Have you declared switchToNoGridView and goBack in the class interface for MainView?
This warning means that the method signatures could not be found in the class of the instance that you are calling the method on; since message dispatch in Objective-C is done at runtime this is allowed, however a warning is shown.
I'm not quite sure from your code, but I think you're encountering one of two errors. Either:
You haven't declared the methods switchToNoGridView or goBack in the MainView class declaration. For Xcode to know that an object responds to a method, you have to include its definition in the class header file, like this:
#interface MainView : ParentObject {
// instance variables
}
// properties
- (void)switchToNoGridView;
- (void)goBack;
#end
This assumes you actually want to declare them as (void), of course - they can have return values, but since you don't do anything with the result of the call in your code, I'm assuming they're void. Or:
You meant to call MainView the class, not mainView the object. Since we can't see your property or instance variable definitions for GridView.h, nor can we see the current method declarations in MainView.h, it's possible that you have a static method +(void)switchToNoGridView and +(void)goBack declared on MainView, but you're calling it on an instance mainView of MainView. For example:
#interface AClass : NSObject { }
+ (void)doSomething;
#end
#implementation AClass
+ (void)doSomething {
NSLog(#"Doing something");
}
#end
#import "AClass.h"
#interface AnotherClass : NSObject {
AClass *aClass;
}
#property(nonatomic,retain) AClass *aClass;
- (void)doSomethingElse;
#end
#implementation AnotherClass
- (void)doSomethingElse {
[aClass doSomething]; // This will break at runtime
[AClass doSomething]; // but this won't
}
#end
Basically, it's possible you've confused class methods with object methods. Either way, you should check your MainView header file for the appropriate method definitions.

Can protocols within protocols be treated as inclusive of the protocol they adopt?

I am assigning protocols in a couple classes that follow an inheritance tree. Like so:
first class
#protocol LevelOne
- (void) functionA
#end
#interface BaseClass : NSObject <LevelOne> {
}
second class
#protocol LevelTwo <LevelOne>
- (void) functionB
#end
#interface SubClass : BaseClass <LevelTwo> {
}
Later I am assigning the class as delegate properties of other classes
base class
#interface AppClass : NSObject {
#protected
id<LevelOne> levelOneDelegate;
}
#property (assign) id<LevelOne> levelOneDelegate;
subclass
#interface AppClassesFriend : AppClass {
#protected
id<LevelTwo> levelTwoDelegate;
}
#property (assign) id<LevelTwo> levelTwoDelegate;
At the end of this journey, AppClassesFriend has 2 properties on it.
"levelOneDelegate" has access to "functionA", when it is assigned with a BaseClass object.
However, I am finding that "levelTwoDelegate" only has access to "functionB" (it is assigned with a SubClass object).
In order to have AppClassesFriend be able to use both functions, it seems I need to assign BOTH a levelOneDelegate AND levelTwoDelegate.
Is there any way to make "levelTwoDelegate" have access to both? Since, both functions are available on "SubClass".
So, what I would like to be able to do is :
SubClass *s = [SubClass alloc];
AppClassesFriend *a = [AppClassesFriend alloc];
a.levelTwoDelegate = s;
so inside AppClassesFriend (a) I could use :
[self.levelTwoDelegate functionA]; <---- this is never found
[self.levelTwoDelegate functionB];
but it seems I have to add
a.levelOneDelegate = s;
Thanks if anyone took the time to read all the way down this far. So in summary the question is, how do I get "levelTwoDelegate" to have access to both functionA and functionB?
Simply declare that your subclass's delegate property implements both level one and level two protocols (i.e. implements both functionA and functionB):
#interface AppClassesFriend : AppClass {
#protected
id<LevelOne,LevelTwo> levelOneAndTwoDelegate;
}
#property (assign) id<LevelOne,LevelTwo> levelOneAndTwoDelegate;

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