Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8754453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:37:27+00:00 2026-06-13T13:37:27+00:00

Let’s say I have 2 classes, A and B. A is a singleton. I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:37:28+00:00Added an answer on June 13, 2026 at 1:37 pm

    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

    sharedInstance.classBReference = self;
    

    is exactly the same as

    [sharedInstance setClassBReference:self];
    

    The problem is that you don’t have any such method -setClassBReference:. In order to set the instance variable, you must instead write

    sharedInstance->classBReference = self;
    

    @protected variables

    After switching your line with this one, you may (if you haven’t made it @public) see the error

    Instance variable ‘classBReference‘ is private

    In this case, you need to alter your classA interface so that classBReference is declared to be @public. Your list of instance variables in classA should look something like

    @interface classA : NSObject
    {
    //@protected
    //(The @protected keyword is optional when at the beginning of the list; instance
    //variables are protected by default, which is why you're needing to declare your
    //instance variable classBReference to be @public (since classB is not a subclass
    //of classA and consequently cannot access its protected instance variables).
        //....
        //some protected instance variables
        //....
    @private
        //....
        //some private instance variables
        //....
    @public
        //....
        //some public instance variables
        classB *classBReference;
        //....
    @protected
        //....
        //some more protected instance variables
        //Note that @protected is not optional in order to make the instance variables
        //here be protected since they are declared subsequent to the prior @public.
        //....
    }
    //....
    @end
    

    Using @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 classA interface:

    @interface classA : NSObject
    {
        classB *classBReference;
    }
    @property classB *classBReference;
    @end
    

    and synthesize the classBReference property to access the classBReference instance variable in classA‘s implementation as follows:

    @implementation classB
    
    @synthesize classBReference = classBReference;
    

    The general set-up

    The @synthesize is 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) @interface one declares an instance variable (“myVariable” in this example) and a property (“myProperty” in this example).

    @interface MyObject : NSObject
    {
        SomeObject *myVariable;
    }
    @property SomeObject *myProperty;
    @end
    

    In the class’s @implementation one has the line

    @synthesize myProperty = myVariable.
    

    The result of this code is that, given an instance

    MyObject *object = //...
    

    of the class, one is able to write

    SomeObject *someObject = //...
    [object setMyProperty:someObject];
    

    and

    SomeObject *someOtherObject = [object myProperty];
    

    The result of calling -setMyProperty: on the instance of MyObject is that myVariable is set equal to the argument passed into the method–in this case someObject. Similarly, the result of calling -myProperty on the instance of MyObject is that myVariable is returned.

    What does it get us?

    Without the @property and @synthesize directives, one would have to declare the methods

    - (void)setMyProperty:(SomeObject *)myProperty;
    - (SomeObject *)myProperty;
    

    manually and define them manually as well:

    - (void)setMyProperty:(SomeObject *)myProperty
    {
        myVariable = myProperty;
    }
    
    - (SomeObject *)myProperty
    {
        return myVariable;
    }
    

    The @property and @synthesize provide 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 @property and @synthesize directives. For a start, not only can you write @synthesize myProperty; omitting the variable name, you can omit the synthesizing of myProperty entirely, 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

    [object setMyProperty:someObject];
    

    you are now able to write

    object.myProperty = someObject;
    

    Similarly, rather than having to write

    SomeObject *someOtherObject = [object myProperty];
    

    you are now able to write

    SomeObject *someOtherObject = object.myProperty;
    

    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.myProperty equal to someObject“, that is not the case. In particular, when we execute the line

    object.myProperty = someObject;
    

    the method

    - (void)setMyProperty:(SomeObject *)someObject
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say I have a table with a Color column. Color can have various
Let's say I have the following classes : public class MyProductCode { private String
Let's say I'm building a data access layer for an application. Typically I have
Let's say I have 2 windows in my application, and two classes responsible for
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a method in java, which looks up a user in
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.