Let’s say I have 2 classes, A and B. A is a singleton. I declare A in B, so I can access the singletons vars in methods in B.
B then creates an instance of another class, say class C.
C Then creates an instance of another class, say class D.
What I need to do is run a method in the instance of class B, from class D, and that’s what is driving me nuts.
My first thought was to put a reference to the instance of class b, in my singleton (class A), something like…
sharedInstance.classBReference = self;
..and then declare the singleton in Class D, and then use something like this in class D instance…
[sharedInstance.classBInstance classBInstanceMethod];
But of course as soon as I did..
classB *classBReference;
In the header of my singleton, it game me the “unknown type” which I read about on here, so instead, I put a
@class classB;
above the @interface, and then I was able to declare…
classB *classBReference;
Without an error of unknown type, but in the init method of class B, this…
sharedInstance.classBReference = self;
Still gives me an error of type
“property classBReference not found on objet of type “class A*” (the singleton) did you mean to access ivar classBReference?”
And I have no idea why it’s doing that, what’s the solution? or is there a better way to do what I’m trying to do?
Dots and Arrows
The “dot notation” is a somewhat recent addition to Objective-C and provides a shorthand notation for accessors. If you have a pointer to an object (or a struct!), you cannot access its instance variables with
.but only with->.Your line
is exactly the same as
The problem is that you don’t have any such method
-setClassBReference:. In order to set the instance variable, you must instead write@protected variables
After switching your line with this one, you may (if you haven’t made it
@public) see the errorIn this case, you need to alter your
classAinterface so thatclassBReferenceis declared to be@public. Your list of instance variables inclassAshould look something likeUsing @properties
The case of classBReference
That being said, it is widely regarded as a better practice to use accessors rather than instance variables in general. In order to do this, you should add a property to your
classAinterface:and synthesize the
classBReferenceproperty to access theclassBReferenceinstance variable inclassA‘s implementation as follows:The general set-up
The
@synthesizeis somewhat unclear on account of the fact that we have both an instance variable and a property with the same name. Some clarification is in order. In general, in a class’s (“MyObject” in this example)@interfaceone declares an instance variable (“myVariable” in this example) and a property (“myProperty” in this example).In the class’s
@implementationone has the lineThe result of this code is that, given an instance
of the class, one is able to write
and
The result of calling
-setMyProperty:on the instance ofMyObjectis thatmyVariableis set equal to the argument passed into the method–in this casesomeObject. Similarly, the result of calling-myPropertyon the instance ofMyObjectis thatmyVariableis returned.What does it get us?
Without the
@propertyand@synthesizedirectives, one would have to declare the methodsmanually and define them manually as well:
The
@propertyand@synthesizeprovide some abridgment to this code. The amount of code that is generated for you becomes even more beneficial when you use various of the property attributes.Note: There is more to say about the
@propertyand@synthesizedirectives. For a start, not only can you write@synthesize myProperty;omitting the variable name, you can omit the synthesizing ofmyPropertyentirely, and the variable names that are used automatically are different from one another in these two cases.A Bit More on Dot Notation
The dot notation from your question provides another layer of abbreviation. Rather than having to write
you are now able to write
Similarly, rather than having to write
you are now able to write
It is important to note that this is just just notation. Though it “kinda looks like” we’re doing simple assignment when we “set
object.myPropertyequal tosomeObject“, that is not the case. In particular, when we execute the linethe method
is executed. For this reason, dot notation is a subject of some contention. It is a convenience, but it is important to keep in mind what your code is doing.