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 7855573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:25:38+00:00 2026-06-02T20:25:38+00:00

Please help with understanding how #import works without using inheritance. So I have created

  • 0

Please help with understanding how #import works without using inheritance.

So I have created a Class named Person and another Class named Body (both classes are inherited from NSObject).

The Body class has 3 ivars (set with properties)

  int hands;
  int feet;
  int legs;

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.

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.

Obviously I’m a struggling newbe but I really want to understand this problem –

  #import <Foundation/Foundation.h>
    #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 <Foundation/Foundation.h>
#import "Body.h"

@interface Person : NSObject {

    Body *body;

}

   @property (assign) Body *body;

@end


#import "Person.h"

@implementation Person

  @synthesize  body;

@end


#import <Foundation/Foundation.h>

@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
  • 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-02T20:25:39+00:00Added an answer on June 2, 2026 at 8:25 pm

    My guess is that you haven’t assigned a Body object to person.body anywhere.

    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.

    So somewhere (most likely in Person‘s designated initialiser), you’ll want to actually create a Body object and assign it to the body property.

    If you don’t do this, then the memory location will remain at 0, which is equivalent to nil. When you chain the assignment here:

    int feet = person.body.feet = 2;
    

    …you’re assigning 2 to both feet and person.body.feet, but since person.body is nil, the setter message to it is ignored. When later you try to log person.body.feet, again, it will be nil and return nil/0.

    Edit: Having looked at your update, yes, this is your problem.

    This line does not create a Body object:

    Body *body;
    

    It creates an instance variable for a pointer to a Body object.

    This line does not create a Body object:

    @property (assign) Body *body;
    

    It declares a property, which is syntactic sugar for getter and setter method declarations.

    This line does not create a Body object:

    @synthesize  body;
    

    It synthesises your previously declared property by creating the getter and setter methods.

    You need to actually create and initialise a Body object and then assign it to your Person object’s body property. For instance:

    - (id)init {
        if (self = [super init]) {
            self.body = [[Body alloc] init];
        }
        return self;
    }
    

    Also, as you aren’t using ARC, you should almost certainly declare that property to be retain not assign, and release it in your Person‘s dealloc method.

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

Sidebar

Related Questions

Hi I did get how the Counting Semaphore works? Please help me in understanding.
Please help with understanding php regular expressions. Have this string: <a href=/kop/product/acer-timeline-5810tg-944g50mi class=imagecache imagecache-product_list
I need some help understanding how jQuery stores elements. Please have a look at
I have a problem with understanding the following events, please help. I have an
Please help me in understanding this piece of code. var person = { 'first-name':
Please help me with my understanding. Also I am not talking about SSL or
Please help me with using the DrScheme built-in function filter. create a function hello
please help me in understanding the problem with replacing the child node $dom =
Can someone please point me towards some nice resources for understanding and using nested
Please help me understanding this: When home button is pressed and iPhone application goes

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.