Access variable that is declared in class's method def from subclass's method def

Discussion in 'iOS Programming' started by moonman239, Jul 14, 2014.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    Alice is a class. Bob is a subclass of Alice. Alice has a method in which a variable named Carol is declared. In Bob's implementation, the aforementioned method is overridden. I want Bob to be able to access Carol. Considering I can safely modify Alice's files, what is the best way to approach this problem?

    By the way, I am using the iOS 7 SDK.
     
  2. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #2
    I don't know if its just me but thing thing of alice and bob is confusing and by the end of your post I don't know who is who anymore.

    But you mean to call super?
     
  3. chown33, Jul 14, 2014
    Last edited: Jul 14, 2014

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    Code:
    // Code #1
    @interface Alice : NSObject
    
    @end
    
    Code:
    // Code #2
    @interface Bob : Alice
    
    @end
    
    Scratch code #1. Replace with:
    Code:
    // Code #3
    @interface Alice : NSObject
    
    -(void) aMethod;
    
    @end
    
    @implementation Alice
    
    -(void) aMethod
    {
       // declare Carol here
    }
    
    @end
    

    Code:
    // Code #4
    @implementation Bob
    
    -(void) aMethod
    {
       // access Carol here
    }
    
    @end
    
    It's impossible to say from your description alone.

    This is because in code #3, where Carol is declared inside aMethod, there are several different ways of "declaring Carol". In some of them, it's not possible at all to declare Carol in a way that makes it accessible in Bob, except by changing the lifetime of Carol. In others, the lifetime of Carol is fine, but the scope would need to changed to make Carol accessible to Bob. So it depends entirely on exactly what the actual code is that "declares Carol". Since you didn't post code, no one knows the answer.

    If you had posted actual example code for Alice, as in code #3, with actual syntactically correct code for the body of aMethod, it would be simple to answer the question. Because then we could see the actual code in question.

    As it now stands, I have to post code I wrote using your description, which may or may not accurately reflect what you think the description says. Furthermore, I have no clear idea what your intent is for declaring Carol. Carol could be an automatic variable, it could be a local static variable, it could be an extern variable, it could even be an instance variable. Those are four distinct possibilities, and each one has a different solution.

    When you post actual code, we can see what you're talking about.

    When you don't, we have to guess what the code is, and hope your description is complete and accurate. When the description is incomplete or inaccurate, the question is unanswerable until you either provide code that's complete and accurate, or someone else provides code that you affirm is accurate.

    Finally, none of the code I wrote was difficult, so I don't see why you're incapable of providing it.
     
  4. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #4
    You are correct.
     

Share This Page