In my code I have a class called 'ProfileShareViewController', In which I have imported another class I have created called 'OwnProfileData', And I have also created an Instance of that class (class = OwnProfileData) as a property Of 'ProfileShareViewController' and synthesized it (instance called 'OwnProfile').
In another class I have called 'EditProfileViewController', I have imported the 'ProfileShareViewController', and now I am trying to change a property of the OwnProfile object from the ProfileShareViewController within the EditProfileViewController class.
For some reason that doesn't work. I have Tried typing:
[[ProfileShareViewController ownProfile] setName:#"Ido"];
(The property I am trying to set is Name, and as it is synthesized in OwnProfileData, I am using 'setName').
This doesn't work and I get the warning: "No known class method for selector 'ownMethod'.
Any Idea as for why that might happen and how I can fix this?
Thanks for your comments! Any support is highly appreciated!
You need an instance of ProfileShareViewController, because ownProfile is an instance property, not an class method. Read about the differences between classes and instances.
Or did I misunderstood smth?
Related
I know that for an instance variable all I have to do is put it inside the initialise method in the instance side and assign it a default value. But how I do this for class variable ? I tried to create an initialise method at class side but it did not give my variable a default value so I had to do this in one of my methods
pythonString ifNil:[pythonString := '']
But I don't like this approach.
I also found this for squeak , http://forum.world.st/Howto-initialize-class-variables-td1667813.html again I don't like this approach either. Is there a proper way of doing this. In Python it was fairly simple case of assignment why is it so cryptic for Pharo ?
First of all I hope that you are talking about instance variable of a class object (not a thing that you define on instance side as "class variable").
initialize is working, but it's being run upon instance creation. And instance (a class object) exists already when you define initialize method.
So when you define your class for the first time, you should run it by yourself e.g. YourClass initialize, bun later each time you load your class into system it should be initialised.
In my code, i have a class , mainClass , which has an instance method -(void)record .
In the interface of mainClass , i have instance variable,which used by this method.
Now, i know that every time i am creating a new instance of the class with :
mainClass *instance=[mainClass alloc];
its creating a new place in memory to all this class variables , and now if i do
[instance record];
it will create all the variables that are in record but they will be new once.
Now lets say i want to call from an outside class to record , and change/use its variables
not create new once, but use the once already created in the mainClass.
whats the best way of doing this, and what it has to do with a class method ?
Should this method be a class method? if yes , why ?
If you want it accessible, instance and permanent changed you have to make it static, will answer your next question
Objective C Static Class Level variables
I have a class that has two classes A and B added to it. In a method in class A, I am trying to call a class B method
Let's assume that the parent class is debugZoneScene, debugZoneLayer is class A and tetraCounter is class B.
Here is a method from debugZoneLayer (class A):
-(void) getHeroVel {
DebugZoneScene *debugZoneScene = (DebugZoneScene*)self.parent;
[debugZoneScene.tetraCounter setTetras];
}
It calls the method, but I get the warning:
'-[DebugZoneLayer getHeroVel]':
'CCNode' may not respond to '-setTetras' (Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments.)
I've tried Googling this, but I couldn't really find anything that related exactly to my problem. I am using Cocos2D, but I think this problem doesn't have anything to do directly with that, and can still be resolved having knowledge in Objective C. Any ideas?
The compiler is telling you that it thinks that debugZoneScene.tetraCounter is an object of type CCNode, not whatever your ClassB is. Check how tetraCounter is declared and allocated in DebugZoneScene.
You can make the warning go away by casting:
[(ClassB *)(debugZoneScene.tetraCounter) setTetras];
this tells the compiler that you don't care what it thinks and you're sure that the object is ClassB. This doesn't solve the actual problem, however.
Your pseudo really fits you... without more details about the signature of setTetras, it will be quite difficult to guess what is wrong in your code ^^
Anyway did you #import the header for TatraCounter class declaration, so that the file where you wrote this code knows about the methods available (and their signature) for the TetraCounter objects?
Just want to check: If you have a class that uses a method added to an existing objective-c object (i.e. NSArray) that you must define the category before the class that uses the category method. By accident I had done this the wrong way round and got the rather cryptic error ...
warning: cannot pass object of non-POD type 'void'
through variadic function; call will abort at runtime
Moving the category before my using class removed the error, a fairly simple case of define it before you use it I guess, but I just wanted to check.
many thanks
gary
As with everything in Objective-C (and plain C), things have to be declared before they are used. That means that if you want to use a function, class, category, struct or anything else in an implementation, you have to import the appropriate header file that declares it.
The order in which they are defined is irrelevant as long as the appropriate declarations are there.
I want to create a Class object in this way:
Class c = [Class classNamed:[NSString stringWithFormat:#"%sview", [_shapeClass name]]];
My problem is this error message: "error: expected expression before 'Class'".
What do you think? What's the problem? Thank you for replys.
Edit:
I want to do this in a switch case.
The problem is that there's no such class as Class (and as a side note, this nonexistent class also doesn't respond to classNamed:).
It's not quite clear whether you want to create a class or get a reference to a class. If the latter, you want the function NSClassFromString(). If you want to dynamically create a class at runtime, you'll need to use the Objective-C runtime functions to create a class, register it and add methods as appropriate.
There is no class named "Class". "Class" is the type of a pointer to a class object. Anyway, there is a classNamed: method in NSBundle, which might be what you were looking for:
Class c = [[NSBundle mainBundle] classNamed:[NSString stringWithFormat:#"%sview", [_shapeClass name]]];
If you know the class is already loaded, you can also get it like this:
Class c = NSClassFromString([NSString stringWithFormat:#"%sview", [_shapeClass name]]);