Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My guess is that you haven't assigned a <code>Body</code> object to <code>person.body</code> anywhere.</p> <p>All objects in Objective-C are allocated on the heap. All those asterisks denote that the variables are pointers, which are mere numbers pointing to memory locations where the objects live. When you declare a property that holds an object, all you are really doing is creating a place to store a pointer to the object. You aren't creating the object itself.</p> <p>So somewhere (most likely in <code>Person</code>'s designated initialiser), you'll want to actually create a <code>Body</code> object and assign it to the <code>body</code> property.</p> <p>If you <em>don't</em> do this, then the memory location will remain at <code>0</code>, which is equivalent to <code>nil</code>. When you chain the assignment here:</p> <pre><code>int feet = person.body.feet = 2; </code></pre> <p>...you're assigning <code>2</code> to both <code>feet</code> and <code>person.body.feet</code>, but since <code>person.body</code> is <code>nil</code>, the setter message to it is ignored. When later you try to log <code>person.body.feet</code>, again, it will be <code>nil</code> and return <code>nil</code>/<code>0</code>.</p> <p><strong>Edit:</strong> Having looked at your update, yes, this is your problem.</p> <p>This line does not create a <code>Body</code> object:</p> <pre><code>Body *body; </code></pre> <p>It creates an instance variable for a <em>pointer to</em> a <code>Body</code> object.</p> <p>This line does not create a <code>Body</code> object:</p> <pre><code>@property (assign) Body *body; </code></pre> <p>It declares a property, which is syntactic sugar for getter and setter method declarations.</p> <p>This line does not create a <code>Body</code> object:</p> <pre><code>@synthesize body; </code></pre> <p>It synthesises your previously declared property by creating the getter and setter methods.</p> <p>You need to actually create and initialise a <code>Body</code> object and then assign it to your <code>Person</code> object's <code>body</code> property. For instance:</p> <pre><code>- (id)init { if (self = [super init]) { self.body = [[Body alloc] init]; } return self; } </code></pre> <p>Also, as you aren't using ARC, you should almost certainly declare that property to be <code>retain</code> not <code>assign</code>, and release it in your <code>Person</code>'s <code>dealloc</code> method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload