Resolved Property with custom getter is nil, even when the data in the getter is not

Discussion in 'iOS Programming' started by moonman239, Jan 12, 2016.

  1. moonman239, Jan 12, 2016
    Last edited: Jan 13, 2016

    moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    So, in object A, I have a property called "objectB" whose getter is overridden so that it returns a property (parentObject) of another object (element). There seems to be a point within my app wherein the objectB property getter returns nil, even though at that point parentObject is not nil. Here's the getter code:
    Edit: Does it matter if the reference to object B is weak?
    Code:
    -(ElementObject *)objectB
    
    {
    
        return [self.element parentObject];
    
    }
    
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
  3. moonman239, Jan 13, 2016
    Last edited: Jan 13, 2016

    moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    I set a breakpoint in the posted method, and the program did not stop.

    Edit #2: The property in question is declared in a superclass, but is synthesized in a subclass. However, the subclass does not set the property, and at the aforementioned point in time, a superclass method is being used.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Set a breakpoint at the calling location. Step into it from there.
     
  5. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #5
    I just did that. It only goes as far as the property declaration.
     
  6. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #6
    That's only 2 lines of code, not too easy to debug a program with only 2 lines of code.
     
  7. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #7
    If the breakpoint isn't working then that piece of code isn't getting called. Please post more of your code, what you posted is literally useless.
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    This can be confirmed by adding a call to the C function abort() in the method, or one can @throw an exception object. The goal is to have code that can't possibly proceed. Then run the app and see if the abort or exception occurs. If it doesn't, then that's reliable evidence that the code isn't executing, rather than a debugger malfunction.
     
  9. moonman239, Jan 14, 2016
    Last edited: Jan 14, 2016

    moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #9
    Unfortunately, @Dookieman @KarlJay what you said about my code is not true. The calling code absolutely has to be running, because I can set a breakpoint on the calling code and it fires.

    Update: I threw an abort() statement in the method I posted, and the program aborted.
     
  10. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #10
    So you override the getter, did you make a call to Super? (the super getter)
    You say it gets called but it's nil.
    It looks like either it's getting called later than you access it, or it's returning nil.

    Is that right?
     
  11. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #11
    I fixed it! It might have been the @synthesize statement in the subclass.
     

Share This Page