Note that there are some explanatory texts on larger screens.

plurals
  1. PO#import without using inheritance between 2 classes
    primarykey
    data
    text
    <p>Please help with understanding how #import works without using inheritance. </p> <p>So I have created a Class named Person and another Class named Body (both classes are inherited from NSObject).</p> <p>The Body class has 3 ivars (set with properties)</p> <pre><code> int hands; int feet; int legs; </code></pre> <p>I then imported and created an instance of the Body class ( *body) into the Person class. My understanding is that I have now made a 'type' from the Body class. This ivar was then set with properties.</p> <p>In main, I then created an instance from the Person class named person. Main recognizes person.body.feet but I cannot get and set its value directly. The only way it can be done is passing its value into a new int variable and then print out the new variable. </p> <p>Obviously I’m a struggling newbe but I really want to understand this problem -</p> <pre><code> #import &lt;Foundation/Foundation.h&gt; #import "Person.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Person *person = [[Person alloc]init]; // this works and NSLog prints out the value is 2 int feet = person.body.feet = 2; NSLog(@"person.body = %i",feet); // this does not work - NSLog prints out the value is 0; [person.body setFeet:2]; NSLog(@"person.body = %i", person.body.feet ); [person release]; [pool drain]; return 0; } #import &lt;Foundation/Foundation.h&gt; #import "Body.h" @interface Person : NSObject { Body *body; } @property (assign) Body *body; @end #import "Person.h" @implementation Person @synthesize body; @end #import &lt;Foundation/Foundation.h&gt; @interface Body : NSObject { int hands; int feet; int legs; } @property int hands; @property int feet; @property int legs; @end #import "Body.h" @implementation Body @synthesize hands; @synthesize feet; @synthesize legs; @end </code></pre>
    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. 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